Subclassing MKAnnotation cause Set collection doesn’t work










1















I've just found that Set of type MKAnnotation doesn't work as expected.



class MyAnnotation: MKPointAnnotation 
let id: String

init(_ id: String)
self.id = id


override var hash: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// empty


Intersection of m's and n's is empty even so hashes are the same. Please, compare with example below:



class MyAnnotation: Hashable, Equatable 
let id: String

init(_ id: String)
self.id = id


var hashValue: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// id "1", id "2"


Intersection of m's and n's is as expected.



Isn't it weird? Maybe there is something in the middle I don't know nor understand.



Xcode 10.1










share|improve this question
























  • you miss Hashable in first part

    – Sh_Khan
    Nov 12 '18 at 18:57











  • @Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

    – Atsuo Sakakihara
    Nov 12 '18 at 19:32















1















I've just found that Set of type MKAnnotation doesn't work as expected.



class MyAnnotation: MKPointAnnotation 
let id: String

init(_ id: String)
self.id = id


override var hash: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// empty


Intersection of m's and n's is empty even so hashes are the same. Please, compare with example below:



class MyAnnotation: Hashable, Equatable 
let id: String

init(_ id: String)
self.id = id


var hashValue: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// id "1", id "2"


Intersection of m's and n's is as expected.



Isn't it weird? Maybe there is something in the middle I don't know nor understand.



Xcode 10.1










share|improve this question
























  • you miss Hashable in first part

    – Sh_Khan
    Nov 12 '18 at 18:57











  • @Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

    – Atsuo Sakakihara
    Nov 12 '18 at 19:32













1












1








1


1






I've just found that Set of type MKAnnotation doesn't work as expected.



class MyAnnotation: MKPointAnnotation 
let id: String

init(_ id: String)
self.id = id


override var hash: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// empty


Intersection of m's and n's is empty even so hashes are the same. Please, compare with example below:



class MyAnnotation: Hashable, Equatable 
let id: String

init(_ id: String)
self.id = id


var hashValue: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// id "1", id "2"


Intersection of m's and n's is as expected.



Isn't it weird? Maybe there is something in the middle I don't know nor understand.



Xcode 10.1










share|improve this question
















I've just found that Set of type MKAnnotation doesn't work as expected.



class MyAnnotation: MKPointAnnotation 
let id: String

init(_ id: String)
self.id = id


override var hash: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// empty


Intersection of m's and n's is empty even so hashes are the same. Please, compare with example below:



class MyAnnotation: Hashable, Equatable 
let id: String

init(_ id: String)
self.id = id


var hashValue: Int
return id.hash


static func ==(lhs: MyAnnotation, rhs: MyAnnotation) -> Bool
return lhs.hashValue == rhs.hashValue



let m1 = MyAnnotation("1")
let m2 = MyAnnotation("2")
let n1 = MyAnnotation("1")
let n2 = MyAnnotation("2")

m1.hashValue //918
n1.hashValue //918

m2.hashValue //921
n2.hashValue //921

if m1 == n1 && m2 == n2
print(true)

// prints true

let s1 = Set(arrayLiteral: m1, m2)
let s2 = Set(arrayLiteral: n1, n2)

let i = s1.intersection(s2)
// id "1", id "2"


Intersection of m's and n's is as expected.



Isn't it weird? Maybe there is something in the middle I don't know nor understand.



Xcode 10.1







ios swift inheritance set mapkit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:57







Atsuo Sakakihara

















asked Nov 12 '18 at 18:44









Atsuo SakakiharaAtsuo Sakakihara

5226




5226












  • you miss Hashable in first part

    – Sh_Khan
    Nov 12 '18 at 18:57











  • @Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

    – Atsuo Sakakihara
    Nov 12 '18 at 19:32

















  • you miss Hashable in first part

    – Sh_Khan
    Nov 12 '18 at 18:57











  • @Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

    – Atsuo Sakakihara
    Nov 12 '18 at 19:32
















you miss Hashable in first part

– Sh_Khan
Nov 12 '18 at 18:57





you miss Hashable in first part

– Sh_Khan
Nov 12 '18 at 18:57













@Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

– Atsuo Sakakihara
Nov 12 '18 at 19:32





@Sh_Khan MKAnnotation conforms to Equatable and Hashable by default.

– Atsuo Sakakihara
Nov 12 '18 at 19:32












2 Answers
2






active

oldest

votes


















0














In first code, you haven't use Equatable protocol but in second code you have used Equatable protocol so it is working.



Equatable protocol is used for comparison. You can refer below link for more information:



https://useyourloaf.com/blog/swift-equatable-and-comparable/






share|improve this answer


















  • 1





    But MKPointAnnotation conforms to Equatable and Hashable.

    – Mike Taverne
    Nov 12 '18 at 19:06


















0














The solution is to override isEqual(_ object: Any?) -> Bool.



Because MKAnnotation is derived from NSObject we need to override NS's method. If we dig into implementation (^ + ) we would find:




Subclasses of NSObject can customize Equatable conformance by overriding isEqual(_:). If two objects are equal, they must have the same hash value, so if you override isEqual(_:), make sure you also override the hash property.







share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268268%2fsubclassing-mkannotation-cause-set-collection-doesn-t-work%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In first code, you haven't use Equatable protocol but in second code you have used Equatable protocol so it is working.



    Equatable protocol is used for comparison. You can refer below link for more information:



    https://useyourloaf.com/blog/swift-equatable-and-comparable/






    share|improve this answer


















    • 1





      But MKPointAnnotation conforms to Equatable and Hashable.

      – Mike Taverne
      Nov 12 '18 at 19:06















    0














    In first code, you haven't use Equatable protocol but in second code you have used Equatable protocol so it is working.



    Equatable protocol is used for comparison. You can refer below link for more information:



    https://useyourloaf.com/blog/swift-equatable-and-comparable/






    share|improve this answer


















    • 1





      But MKPointAnnotation conforms to Equatable and Hashable.

      – Mike Taverne
      Nov 12 '18 at 19:06













    0












    0








    0







    In first code, you haven't use Equatable protocol but in second code you have used Equatable protocol so it is working.



    Equatable protocol is used for comparison. You can refer below link for more information:



    https://useyourloaf.com/blog/swift-equatable-and-comparable/






    share|improve this answer













    In first code, you haven't use Equatable protocol but in second code you have used Equatable protocol so it is working.



    Equatable protocol is used for comparison. You can refer below link for more information:



    https://useyourloaf.com/blog/swift-equatable-and-comparable/







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 18:54









    Hardik HalaniHardik Halani

    1219




    1219







    • 1





      But MKPointAnnotation conforms to Equatable and Hashable.

      – Mike Taverne
      Nov 12 '18 at 19:06












    • 1





      But MKPointAnnotation conforms to Equatable and Hashable.

      – Mike Taverne
      Nov 12 '18 at 19:06







    1




    1





    But MKPointAnnotation conforms to Equatable and Hashable.

    – Mike Taverne
    Nov 12 '18 at 19:06





    But MKPointAnnotation conforms to Equatable and Hashable.

    – Mike Taverne
    Nov 12 '18 at 19:06













    0














    The solution is to override isEqual(_ object: Any?) -> Bool.



    Because MKAnnotation is derived from NSObject we need to override NS's method. If we dig into implementation (^ + ) we would find:




    Subclasses of NSObject can customize Equatable conformance by overriding isEqual(_:). If two objects are equal, they must have the same hash value, so if you override isEqual(_:), make sure you also override the hash property.







    share|improve this answer



























      0














      The solution is to override isEqual(_ object: Any?) -> Bool.



      Because MKAnnotation is derived from NSObject we need to override NS's method. If we dig into implementation (^ + ) we would find:




      Subclasses of NSObject can customize Equatable conformance by overriding isEqual(_:). If two objects are equal, they must have the same hash value, so if you override isEqual(_:), make sure you also override the hash property.







      share|improve this answer

























        0












        0








        0







        The solution is to override isEqual(_ object: Any?) -> Bool.



        Because MKAnnotation is derived from NSObject we need to override NS's method. If we dig into implementation (^ + ) we would find:




        Subclasses of NSObject can customize Equatable conformance by overriding isEqual(_:). If two objects are equal, they must have the same hash value, so if you override isEqual(_:), make sure you also override the hash property.







        share|improve this answer













        The solution is to override isEqual(_ object: Any?) -> Bool.



        Because MKAnnotation is derived from NSObject we need to override NS's method. If we dig into implementation (^ + ) we would find:




        Subclasses of NSObject can customize Equatable conformance by overriding isEqual(_:). If two objects are equal, they must have the same hash value, so if you override isEqual(_:), make sure you also override the hash property.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 6 '18 at 14:11









        Atsuo SakakiharaAtsuo Sakakihara

        5226




        5226



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268268%2fsubclassing-mkannotation-cause-set-collection-doesn-t-work%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo