Subclassing MKAnnotation cause Set collection doesn’t work
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
add a comment |
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
you miss Hashable in first part
– Sh_Khan
Nov 12 '18 at 18:57
@Sh_KhanMKAnnotation
conforms toEquatable
andHashable
by default.
– Atsuo Sakakihara
Nov 12 '18 at 19:32
add a comment |
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
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
ios swift inheritance set mapkit
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_KhanMKAnnotation
conforms toEquatable
andHashable
by default.
– Atsuo Sakakihara
Nov 12 '18 at 19:32
add a comment |
you miss Hashable in first part
– Sh_Khan
Nov 12 '18 at 18:57
@Sh_KhanMKAnnotation
conforms toEquatable
andHashable
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
add a comment |
2 Answers
2
active
oldest
votes
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/
1
ButMKPointAnnotation
conforms to Equatable and Hashable.
– Mike Taverne
Nov 12 '18 at 19:06
add a comment |
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 overridingisEqual(_:)
. If two objects are equal, they must have the same hash value, so if you overrideisEqual(_:)
, make sure you also override thehash
property.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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/
1
ButMKPointAnnotation
conforms to Equatable and Hashable.
– Mike Taverne
Nov 12 '18 at 19:06
add a comment |
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/
1
ButMKPointAnnotation
conforms to Equatable and Hashable.
– Mike Taverne
Nov 12 '18 at 19:06
add a comment |
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/
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/
answered Nov 12 '18 at 18:54
Hardik HalaniHardik Halani
1219
1219
1
ButMKPointAnnotation
conforms to Equatable and Hashable.
– Mike Taverne
Nov 12 '18 at 19:06
add a comment |
1
ButMKPointAnnotation
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
add a comment |
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 overridingisEqual(_:)
. If two objects are equal, they must have the same hash value, so if you overrideisEqual(_:)
, make sure you also override thehash
property.
add a comment |
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 overridingisEqual(_:)
. If two objects are equal, they must have the same hash value, so if you overrideisEqual(_:)
, make sure you also override thehash
property.
add a comment |
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 overridingisEqual(_:)
. If two objects are equal, they must have the same hash value, so if you overrideisEqual(_:)
, make sure you also override thehash
property.
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 overridingisEqual(_:)
. If two objects are equal, they must have the same hash value, so if you overrideisEqual(_:)
, make sure you also override thehash
property.
answered Dec 6 '18 at 14:11
Atsuo SakakiharaAtsuo Sakakihara
5226
5226
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
you miss Hashable in first part
– Sh_Khan
Nov 12 '18 at 18:57
@Sh_Khan
MKAnnotation
conforms toEquatable
andHashable
by default.– Atsuo Sakakihara
Nov 12 '18 at 19:32