equatable

Difference between using ObjectIdentifier() and '===' Operator

笑着哭i 提交于 2019-12-08 23:32:23
问题 Let's say I'm implementing a root class in Swift, which I declare adopts the Equatable protocol (I want to be able to tell if an array of my type contains a given instance or not). What is the difference -if any, in this specific case- between implementing the protocol's required == operator as: public static func ==(lhs: MyClass, rhs: MyClass) -> Bool { return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) } ...as opposed to just doing this: public static func ==(lhs: MyClass, rhs: MyClass)

Remove duplicates from a multi-dimensional array

╄→гoц情女王★ 提交于 2019-12-08 06:08:53
问题 I wrote the following extension to remove duplicates from my Array. extension Array where Element : Equatable{ func removeDups() -> [Element]{ var result = [Element]() for element in self{ if !result.contains(element){ result.append(element) } } return result } } Linear Array let linearArr = [1,2,2,3] linearArr.removeDups() // [1,2,3] works well! Multi dimensional Array let multiDimArr : [[Int]] = [[1,2,3], [1,2,3], [1,2 ,4]] multiDimArr.removeDups() // Error! Type [Int] does not conform to

Common Equatable class on Swift

孤者浪人 提交于 2019-12-08 01:06:49
问题 I need container for Any Equatable items in NOT Generic class (for example UI classes initial from storyboard). I need like this var items: [Equatable]? but it don't work, Equatable need Generic . the problem that there is no exist common Equatable class. Ok - Go to generic! But if I do this class Item<Value: Equatable>: Equatable { var value: Value init(_ value: Value) { self.value = value } //Equatable public static func ==(lhs: Item, rhs: Item) -> Bool { return (lhs.value == rhs.value) } }

AnyObject try cast to Equatable

≯℡__Kan透↙ 提交于 2019-12-07 15:05:18
问题 I have an Equatable class class Item: Equatable { var value: AnyObject? var title: String init(title: String, value: AnyObject?) { self.title = title self.value = value } //Equatable public static func ==(lhs: Item, rhs: Item) -> Bool { return ((lhs.title == rhs.title) && (lhs.value === rhs.value)) } } But I want cast try var value to Equatable, So that get soft equatable result if let lValue = lhs.value as? Equatable, // Error let rValue = rhs.value as? Equatable { // Error valueEq = (lValue

Remove duplicates from a multi-dimensional array

夙愿已清 提交于 2019-12-07 09:57:26
I wrote the following extension to remove duplicates from my Array. extension Array where Element : Equatable{ func removeDups() -> [Element]{ var result = [Element]() for element in self{ if !result.contains(element){ result.append(element) } } return result } } Linear Array let linearArr = [1,2,2,3] linearArr.removeDups() // [1,2,3] works well! Multi dimensional Array let multiDimArr : [[Int]] = [[1,2,3], [1,2,3], [1,2 ,4]] multiDimArr.removeDups() // Error! Type [Int] does not conform to Equatable I read here . And the answer says Array comparisons using == should work. It doesn't work all

Common Equatable class on Swift

假装没事ソ 提交于 2019-12-06 11:15:19
I need container for Any Equatable items in NOT Generic class (for example UI classes initial from storyboard). I need like this var items: [Equatable]? but it don't work, Equatable need Generic . the problem that there is no exist common Equatable class. Ok - Go to generic! But if I do this class Item<Value: Equatable>: Equatable { var value: Value init(_ value: Value) { self.value = value } //Equatable public static func ==(lhs: Item, rhs: Item) -> Bool { return (lhs.value == rhs.value) } } then I will be forced to specify the type in my nonGeneric-UI class. Like this var items: [Item

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject [duplicate]

橙三吉。 提交于 2019-12-05 13:25:18
问题 This question already has answers here : NSObject subclass in Swift: hash vs hashValue, isEqual vs == (2 answers) Closed 3 years ago . The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is overriding this custom one? Note: Because FooBar must subclass from NSCoding and NSObject, FooBar does not list

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject [duplicate]

↘锁芯ラ 提交于 2019-12-03 22:51:36
This question already has answers here : Closed 3 years ago . NSObject subclass in Swift: hash vs hashValue, isEqual vs == (2 answers) The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is overriding this custom one? Note: Because FooBar must subclass from NSCoding and NSObject, FooBar does not list Equatable as a protocol because it causes this error: Redundant conformance of 'FooBar' to protocol

“contextual closure type expects 2 arguments” error when using reduce in Swift 4

China☆狼群 提交于 2019-12-03 13:31:43
问题 The following code compiles in Swift 3 extension Array where Element: Equatable { var removeDuplicate: [Element] { return reduce([]){ $0.0.contains($0.1) ? $0.0 : $0.0 + [$0.1] } } } but produces the error error: contextual closure type '(_, _) -> _' expects 2 arguments, but 1 was used in closure body in Swift 4. How to convert this code to be compiled in Swift 4? 回答1: The closure passed to reduce takes 2 parameters, e.g. $0 and $1 in the shorthand notation: extension Array where Element:

Swift 2.2, Contains Method not working

℡╲_俬逩灬. 提交于 2019-12-01 20:36:04
Contains method not working properly and it is giving me false result even if it is matching with Object? My Code Below class Generic: NSObject, NSCoding { var genericCode: String? var genericName : String? var genericType : String? var genericImageUrl : String? var genericPhone: String? var orgName : String? override init() { self.genericCode = String("") self.genericName = String("") self.genericType = String("") self.genericImageUrl = String("") self.genericPhone = String("") self.orgName = String("") } //Parameterzed Constructor for the Generic init(genericCode: String , genericName: