equatable

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

浪尽此生 提交于 2019-11-30 09:46:25
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) -> Bool { return (lhs === rhs) } As a reference, this is what the documentation says about

Swift Protocol Implements Equatable

空扰寡人 提交于 2019-11-29 22:54:22
I have the the below Protocol : protocol Cacheable { //....// func identifier() -> String } Can I make Cacheable implements Equatable ? when I do the following: extension Cacheable: Equatable {} func ==(lhs:Cacheable,rhs:Cacheable) -> Bool { return lhs.identifier() == rhs.identifier() } I got this error message : Extension of protocol Cacheable cannot have an inheritance clause 1) Allow two Cacheable s of the same type to be compare protocol Cacheable: Equatable { //....// func identifier() -> String } func ==<T : Cacheable>(lhs: T, rhs: T) -> Bool { return lhs.identifier() == rhs.identifier()

Swift Protocol Implements Equatable

随声附和 提交于 2019-11-28 19:47:08
问题 I have the the below Protocol : protocol Cacheable { //....// func identifier() -> String } Can I make Cacheable implements Equatable ? when I do the following: extension Cacheable: Equatable {} func ==(lhs:Cacheable,rhs:Cacheable) -> Bool { return lhs.identifier() == rhs.identifier() } I got this error message : Extension of protocol Cacheable cannot have an inheritance clause 回答1: 1) Allow two Cacheable s of the same type to be compare protocol Cacheable: Equatable { //....// func

How does Dictionary use the Equatable protocol in Swift?

☆樱花仙子☆ 提交于 2019-11-27 21:40:39
问题 In order to solve this question, I have been playing around with a custom struct that implements the Hashable Protocol. I'm trying to see how many times the equivalency operator overload ( == ) gets called depending on if there is a hash collision or not when populating a Dictionary . Update @matt wrote a much cleaner example of a custom struct that implements the Hashable protocol and shows how often hashValue and == get called. I am copying his code below. To see my original example, check

Why must a protocol operator be implemented as a global function?

谁都会走 提交于 2019-11-27 21:06:12
I've seen the answer to this Swift Equatable Protocol question that mentions how the == method must be declared in the global scope. If I don't adopt Equatable , I still could declare == to test for equality between two of my types. // extension Foo: Equatable {} func ==(lhs: Foo, rhs: Foo) -> Bool { return lhs.bar == rhs.bar } struct Foo { let bar:Int } The fact that its implementation needs to be declared at a global scope, makes it seem incidental to and distinct from a protocol, even if Equatable was adopted. How is the Equatable protocol anything more than syntactic sugar that merely lets

How to properly implement the Equatable protocol in a class hierarchy?

◇◆丶佛笑我妖孽 提交于 2019-11-27 17:42:17
问题 I'm trying to implement the == operator (from Equatable ) in a base class and its subclasses in Swift 3. All of the classes will only be used in Swift so I do not want to involve NSObject or the NSCopying protocol. I started with a base class and a subclass: class Base { var x : Int } class Subclass : Base { var y : String } Now I wanted to add Equatable and the == operator to Base . Seems simple enough. Copy the == operator signature from the documentation: class Base : Equatable { var x :