Swift - What is the difference between implementing Hashable and Equatable and NSObject hash and isEqual overrides - ios

I'm trying to wrap my head around the new DiffableDataSource way of handling data in tableviews/collectionviews and during testing I came across a strange crash.
I would expect that the two implementations below would work exactly the same:
Implementation 1:
class DiffableSection {
var id: String
var items: [AnyDiffable]
init(id: String,
items: [AnyDiffable] = []) {
self.id = id
self.items = items
}
}
extension DiffableSection: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
extension DiffableSection: Equatable {
static func == (lhs: DiffableSection, rhs: DiffableSection) -> Bool {
return lhs.id == rhs.id
}
}
Implementation 2:
class DiffableSection: NSObject {
var id: String
var items: [AnyDiffable]
init(id: String,
items: [AnyDiffable] = []) {
self.id = id
self.items = items
}
// MARK: - Hashable and Equatable
public override var hash: Int {
var hasher = Hasher()
hasher.combine(id)
return hasher.finalize()
}
public override func isEqual(_ object: Any?) -> Bool {
guard let object = object as? DiffableSection else { return false }
return id == object.id
}
}
but apparently they are not - Implementation 2 works with the code below and Implementation 1 does not.
func apply(_ sections: [DiffableSection]) {
var snapshot = self.snapshot()
for section in sections {
snapshot.appendSectionIfNeeded(section)
snapshot.appendItems(section.items, toSection: section)
}
apply(snapshot)
}
(...)
extension NSDiffableDataSourceSnapshot {
mutating func appendSectionIfNeeded(_ identifier: SectionIdentifierType) {
if sectionIdentifiers.contains(identifier) { return }
appendSections([identifier])
}
}
The crash message I get when running with Implementation 1:
'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: section != NSNotFound'
Can someone explain me what are the differences in those implementations? How could I fix Implementation 1 to work same as Implementation 2??

Related

Confirm to Equatable for Custom struct

I have below kind of response model, where the body is decided by another variable. How can i confirm equatable to this Model
public struct Model {
let type: String? // can be type1 or type2
let body: ResponseType?
}
protocol ResponseType: Codable {
}
struct Response1: ResponseType {
var items: [String]?
}
struct Response2: ResponseType {
var item: String?
}
What i want to achive:
extension Model: Equatable {
public static func == (lhs: Model, rhs: Model) -> Bool {
// How to equate the body?
}
}
When im trying to add Equatable to ResponseType protocol it says below error.
Protocol 'ResponseType' can only be used as a generic constraint because it has Self or associated type requirements
You need to implement == manually. swift doesn't know that body can only be two types, which are?
public struct Model: Equatable {
public static func == (lhs: Model, rhs: Model) -> Bool {
if lhs.type != rhs.type {
return false
}
if let lhsBody = lhs.body as? Response1, let rhsBody = rhs.body as? Response1 {
return lhsBody == rhsBody
} else if let lhsBody = lhs.body as? Response2, let rhsBody = rhs.body as? Response2 {
return lhsBody == rhsBody
} else {
return false
}
}
let type: String? // can be type1 or type2
let body: ResponseType?
}
protocol ResponseType: Codable {
}
struct Response1: ResponseType, Equatable {
var items: [String]?
}
struct Response2: ResponseType, Equatable {
var item: String?
}
It might be easier if you change Model into an enum:
enum Model: Codable, Equatable {
case type1(items: [String]?)
case type2(item: String)
var type: String {
switch self {
case .type1: return "type1"
case .type2: return "type2"
}
}
}
You probably need to change the Codable implementation so that it encodes and decodes the way you want to.

Storing objects conforming to a protocol with generics in a typed array

I've got a protocol:
protocol Adjustable: Equatable {
associatedtype T
var id: String { get set }
var value: T { get set }
init(id: String, value: T)
}
And a struct that conforms to it:
struct Adjustment: Adjustable {
static func == (lhs: Adjustment, rhs: Adjustment) -> Bool {
return lhs.id == rhs.id
}
typealias T = CGFloat
var id: String
var value: T
}
And I'm building a wrapper class that behaves like a Set to handle an ordered list of these properties:
struct AdjustmentSet {
var adjustmentSet: [Adjustable] = []
func contains<T: Adjustable>(_ item: T) -> Bool {
return adjustmentSet.filter({ $0.id == item.id }).first != nil
}
}
let brightness = Adjustment(id: "Brightness", value: 0)
let set = AdjustmentSet()
print(set.contains(brightness))
But that of course doesn't work, erroring with:
error: protocol 'Adjustable' can only be used as a generic constraint because it has Self or associated type requirements
var adjustmentSet: [Adjustable] = []
Looking around, I thought at first this was because the protocol doesn't conform to Equatable, but then I added it, and it still doesn't work (or I did it wrong).
Moreover, I would like to be able to use a generic here, so that I can do something like:
struct Adjustment<T>: Adjustable {
static func == (lhs: Adjustment, rhs: Adjustment) -> Bool {
return lhs.id == rhs.id
}
var id: String
var value: T
}
let brightness = Adjustment<CGFloat>(id: "Brightness", value: 0)
Or:
struct FloatAdjustment: Adjustable {
static func == (lhs: Adjustment, rhs: Adjustment) -> Bool {
return lhs.id == rhs.id
}
typealias T = CGFloat
var id: String
var value: T
}
let brightness = FloatAdjustment(id: "Brightness", value: 0)
And still be able to store an array of [Adjustable] types, so that eventually I can do:
var set = AdjustmentSet()
if set.contains(.brightness) {
// Do something!
}
Or
var brightness = ...
brightness.value = 1.5
set.append(.brightness)
You can't have an array of items of type Adjustable, because Adjustable isn't really a type. It's a blue print that describes a set of types, one per every possible value of T.
To get around this, you need to use a type eraser https://medium.com/dunnhumby-data-science-engineering/swift-associated-type-design-patterns-6c56c5b0a73a
Have made some great progress using Alexander's suggestion; I was able to use some nested class types to inherit the base type erasure class, and use a generic protocol that conforms to AnyHashable so I can use this with a set!
// Generic conforming protocol to AnyHashable
protocol AnyAdjustmentProtocol {
func make() -> AnyHashable
}
protocol AdjustmentProtocol: AnyAdjustmentProtocol {
associatedtype A
func make() -> A
}
struct AdjustmentTypes {
internal class BaseType<T>: Hashable {
static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
return lhs.name == rhs.name
}
typealias A = T
var hashValue: Int { return name.hashValue }
let name: String
let defaultValue: T
let min: T
let max: T
var value: T
init(name: String, defaultValue: T, min: T, max: T) {
self.name = name
self.defaultValue = defaultValue
self.min = min
self.max = max
self.value = defaultValue
}
}
class FloatType: BaseType<CGFloat> { }
class IntType: BaseType<Int> { }
}
struct AnyAdjustmentType<A>: AdjustmentProtocol, Hashable {
static func == (lhs: AnyAdjustmentType<A>, rhs: AnyAdjustmentType<A>) -> Bool {
return lhs.hashValue == rhs.hashValue
}
private let _make: () -> AnyHashable
private let hashClosure:() -> Int
var hashValue: Int {
return hashClosure()
}
init<T: AdjustmentProtocol & Hashable>(_ adjustment: T) where T.A == A {
_make = adjustment.make
hashClosure = { return adjustment.hashValue }
}
func make() -> AnyHashable {
return _make()
}
}
struct Brightness: AdjustmentProtocol, Hashable {
func make() -> AnyHashable {
return AdjustmentTypes.FloatType(name: "Brightness", defaultValue: 0, min: 0, max: 1)
}
}
struct WhiteBalance: AdjustmentProtocol, Hashable {
func make() -> AnyHashable {
return AdjustmentTypes.IntType(name: "White Balance", defaultValue: 4000, min: 3000, max: 7000)
}
}
let brightness = Brightness().make()
let whiteBalance = WhiteBalance().make()
var orderedSet = Set<AnyHashable>()
orderedSet.insert(brightness)
print(type(of: orderedSet))
print(orderedSet.contains(brightness))
for obj in orderedSet {
if let o = obj as? AdjustmentTypes.FloatType {
print(o.value)
}
if let o = obj as? AdjustmentTypes.IntType {
print(o.value)
}
}
Prints:
Set<AnyHashable>
true
0.0
Special thanks to this article: https://medium.com/#chris_dus/type-erasure-in-swift-84480c807534 which had a simple and clean example on how to implement a generic type eraser.
With Swift 5.7 you will be able to this without any error from the compiler by prefixing your protocol with any, so your set becomes:
struct AdjustmentSet {
var adjustmentSet: [any Adjustable] = []
func contains(_ item: some Adjustable) -> Bool {
return adjustmentSet.first { $0.id == item.id } != nil
}
}
Note that all items in your adjustmentSet array will be allocated on heap since compile time swift can't determine the size of existential type Adjustable as types implementing it will have variable size.

Swift type erasure protocol with required init method

I have the protocol that conform Equatable
protocol TestProtocol: Equatable {
var id: Int {get}
}
func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
return lhs.id == rhs.id
}
To have opportunities to store TestProtocol value we should use type erasure.
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
var id: Int {
return item.id
}
private let item: T
init(_ testProtocol: T) {
self.item = testProtocol
}
}
And, after all, we can use it, like this
struct TestStruct: TestProtocol {
let id: Int
}
let a = TestStruct(id: 1)
let b = TestStruct(id: 1)
a == b /// true
// let a = TestStruct(id: 1)
// let b = TestStruct(id: 0)
// a == b /// false
And all ok. But I want to use TestProtocol with required init method, such as init(id: Int).
How can I implement AnyTestProtocol If TestProtocol contains required init method?
protocol TestProtocol: Equatable {
var id: Int {get}
/// Required init method for every protocol implementation
init(id: Int)
}
func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
return lhs.id == rhs.id
}
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
var id: Int {
return item.id
}
private let item: T
required init(id: Int) {
????????
}
init(_ testProtocol: T) {
self.item = testProtocol
}
}
Just forward the required init(id:) call to T:
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
// ...
required init(id: Int) {
self.item = T(id: id)
}
// ...
}

How can I create a base from this Swift class so that it can be inherited?

I have a class named UserManager.
public class UserManager{
static let sharedInstance = UserManager()
let center = NSNotificationCenter.defaultCenter()
let queue = NSOperationQueue.mainQueue()
var resources = Dictionary<Int, User>()
var clients = Dictionary<Int, Set<String>>()
private init(){
}
private func addToClientMap(id: Int, clientName: String){
if clients[id] == nil {
clients[id] = Set<String>()
clients[id]!.insert(clientName)
}else{
clients[id]!.insert(clientName)
}
}
func getResource(id: Int, clientName: String) -> User?{
if let resource = resources[id] {
addToClientMap(id, clientName: clientName)
return resource
}else{
return nil
}
}
func createResource(data:JSON, clientName: String) -> User? {
if let id = data["id"].int {
if let resource = resources[id] {
addToClientMap(id, clientName: clientName)
return resource
}else{
resources[id] = mapJSONToUser(data) //need to make generic
addToClientMap(id, clientName: clientName)
return resources[id]
}
}
return nil
}
func releaseResource(id: Int, clientName: String){
if clients[id] != nil {
clients[id]!.remove(clientName)
if clients[id]!.count == 0 {
resources.removeValueForKey(id)
clients.removeValueForKey(id)
}
}
}
}
Notice that I have an object called User, and it's used everywhere in this class.
I'd like to have classes called PostManager and AdminManager, which uses the same logic as the class above.
I could simply copy and paste the code above and replace the object User with Post and Admin. But...obviously this is bad practice.
What can I do to this class so that it accepts any resource? Not just User
The most obvious way to do something like this is to embed all of the generic functionality in a generic class, then inherit your UserManager from that:
protocol Managable {
init(json:JSON)
}
public class Manager<T:Manageable> {
let center = NSNotificationCenter.defaultCenter()
let queue = NSOperationQueue.mainQueue()
var resources = Dictionary<Int, T>()
var clients = Dictionary<Int, Set<String>>()
private init(){
}
private func addToClientMap(id: Int, clientName: String){
if clients[id] == nil {
clients[id] = Set<String>()
clients[id]!.insert(clientName)
}else{
clients[id]!.insert(clientName)
}
}
func getResource(id: Int, clientName: String) -> T?{
if let resource = resources[id] {
addToClientMap(id, clientName: clientName)
return resource
}else{
return nil
}
}
func createResource(data:JSON, clientName: String) -> T? {
if let id = data["id"].int {
if let resource = resources[id] {
addToClientMap(id, clientName: clientName)
return resource
}else{
resources[id] = T(json:data) //need to make generic
addToClientMap(id, clientName: clientName)
return resources[id]
}
}
return nil
}
func releaseResource(id: Int, clientName: String){
if clients[id] != nil {
clients[id]!.remove(clientName)
if clients[id]!.count == 0 {
resources.removeValueForKey(id)
clients.removeValueForKey(id)
}
}
}
}
class User : Managable {
required init(json:JSON) {
}
}
class UserManager : Manager<User> {
static var instance = UserManager()
}
Now then, any class that implements the Manageable protocol (ie., it has an init(json:JSON) method can have a Manager class variant. Note that since a generic class can't have a static property, that's been moved into the subclass.
Given that inheritance can hide implementation details, if reference semantics are not needed then a protocol + associated type (generics) implementation using structs might be safer and arguably more "Swifty".
Define your protocol with an associated type (Swift 2.2) or type alias (Swift 2.1):
protocol Manager {
associatedtype MyManagedObject // use typealias instead for Swift 2.1
func getResource(id: Int, clientName: String) -> MyManagedObject?
func createResource(data: JSON, clientName: String) -> MyManagedObject?
func releaseResource(id: Int, clientName: String)
}
And then your implementation becomes:
public struct UserManager: Manager {
typealias MyManagedObject = User
func getResource(id: Int, clientName: String) -> User? { ... }
func createResource(data: JSON, clientName: String) -> User? { ... }
func releaseResource(id: Int, clientName: String) { ... }
}
And you can further add objects using the same protocol easily, specifying what 'MyManagedObject' should be:
public struct PostManager: Manager {
typealias MyManagedObject = Post
func getResource(id: Int, clientName: String) -> Post? { ... }
func createResource(data: JSON, clientName: String) -> Post? { ... }
func releaseResource(id: Int, clientName: String) { ... }
}
I would recommend reading up more on protocols and generics in detail (there are many examples online, Apple's documentation is a good place to start).

Stack implementation in Swift

I'm new to Swift and iOS programming.
I'm trying to test out a simple algorithm and need an array of Stacks. Don't have to be anything fancy (Stacks of Ints will do).
I got the Stack implementation from The Swift Programming Language documentation:
struct IntStack {
var items = [Int]()
mutating func push(item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
mutating func count() -> Int {
return items.count
}
mutating func show() {
println(items)
}
}
The count and show functions are my contribution. But when I try to declare an array of Stacks I get an error...
var lines = IntStack()[5]
"IntStack" does not have a member named subscript
I'm guessing it has something to do with Optionals but can figure out what it is.
Any help?
Details
Swift 5.1, Xcode 11.3.1
Generic Stack Implementation
Stackable protocol
protocol Stackable {
associatedtype Element
func peek() -> Element?
mutating func push(_ element: Element)
#discardableResult mutating func pop() -> Element?
}
extension Stackable {
var isEmpty: Bool { peek() == nil }
}
Stack
struct Stack<Element>: Stackable where Element: Equatable {
private var storage = [Element]()
func peek() -> Element? { storage.last }
mutating func push(_ element: Element) { storage.append(element) }
mutating func pop() -> Element? { storage.popLast() }
}
extension Stack: Equatable {
static func == (lhs: Stack<Element>, rhs: Stack<Element>) -> Bool { lhs.storage == rhs.storage }
}
extension Stack: CustomStringConvertible {
var description: String { "\(storage)" }
}
extension Stack: ExpressibleByArrayLiteral {
init(arrayLiteral elements: Self.Element...) { storage = elements }
}
Usage
var stack = Stack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.peek())
print(stack.pop())
print(stack)
print(stack == Stack<Int>())
stack = [3,2,1]
print(stack)
There's no problem with what you're doing there - that's just not the syntax for declaring an array. If you want an array of 5 stacks, you can do this:
[IntStack(), IntStack(), IntStack(), IntStack(), IntStack()]
Or, you can initialise the array like this:
Array(count: 5, repeatedValue: IntStack())
Also, you don't need to mark your functions as mutating unless they actually mutate the structure - so count() and show() don't need it.
It's possible to just extend arrays with stack specific methods. This might or might not be what you want, depending on if you want to disallow array like access.
protocol Stack {
associatedtype Element
mutating func push(item: Element)
// allows discarding the result without generating a warning.
#discardableResult
mutating func pop() -> Element?
func peek() -> Element?
var count: Int { get }
}
extension Array: Stack {
mutating func push(item: Element) {
self.append(item)
}
mutating func pop() -> Element? {
if let last = self.last {
self.remove(at: self.count - 1)
return last
}
return .none
}
func peek() -> Element? {
self.last
}
}
Simple test case:
class StackTests: XCTestCase {
func testExample() throws {
var stack = Array<Int>()
XCTAssertEqual(stack.peek(), .none, "stack is empty, peek returns none")
XCTAssertEqual(stack.pop(), .none, "stack is empty, pop returns none")
stack.push(item: 0)
stack.push(item: 1)
stack.push(item: 2)
XCTAssertEqual(stack.peek(), 2)
XCTAssertEqual(stack.pop(), 2)
XCTAssertEqual(stack.peek(), 1)
XCTAssertEqual(stack.pop(), 1)
XCTAssertEqual(stack.peek(), 0)
XCTAssertEqual(stack.pop(), 0)
}
}
There is no need to declare the size of the stack when you init it. Jus calling this should be enough.
var lines = IntStack()
Also note that your count() and show() methods should not be mutating since they don't modify the struct in any way.
Just look into this code. Stack example with generic data type and without using the array.
class Node<T>: CustomStringConvertible {
let value: T
var next: Node?
var description: String {
guard let next = next else { return "\(value)" }
return "\(value)\n" + String(describing: next)
}
init(value: T) {
self.value = value
}
}
// Stack class to hold all items
class Stack<T>: CustomStringConvertible {
var top: Node<T>?
var description: String {
guard let top = top else { return "---- Stack is EMPTY ----" }
return "---- Stack Begin ----\n" + String(describing: top) + "\n---- Stack End ----"
}
// push
func push(_ value: T) {
let currentTop = top
top = Node(value: value)
top?.next = currentTop
}
#discardableResult
func pop() -> T? {
let currentTop = top
top = top?.next
return currentTop?.value
}
#discardableResult
func peek() -> T? {
return top?.value
}
}
Excellent implementation! One thought: I think it should be:
func peek() -> Element? { storage.last }
Here is a Stack implementation using Swift Generics,
struct Fruit {
let fruitName : String
let color : String
init(_ name: String,_ color: String) {
self.fruitName = name
self.color = color
}
}
let fruit1 = Fruit("Apple", "Red")
let fruit2 = Fruit("Grapes", "Green")
let fruitStack = Stack<Fruit>()
fruitStack.push(fruit1)
fruitStack.push(fruit2)
let fruitFfromStack = fruitStack.pop()
print("Fruit popped from Stack, Name : \(String(describing: fruitFfromStack?.fruitName)) ,Color : \(String(describing: fruitFfromStack?.color))")
let fruitFfromStack1 = fruitStack.pop()
print("Fruit popped from Stack, Name : \(String(describing: fruitFfromStack1?.fruitName)) ,Color : \(String(describing: fruitFfromStack1?.color))")
Full code is here :
https://reactcodes.blogspot.com/2019/01/generic-stack-implementation-with.html

Resources