Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

objective c - Adding Strings to NsMutableSets

If I have two string objects that both have value "hi", and I add them to an NSMutableSet, are they treated as different objects or not? Thanks a bunch!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Beside the correct comment of @rmaddy, there is a fundamental problem with it: It is simply mostly impossible to guarantee that no objects are equal in a set. (It is easy to guarantee that the are not identical.) This would imply to recheck the equaty of all objects (expensive), when one changes (mostly impossible to detect). This is, because NSSet does not copy its content objects.

Let's have an example:

NSMutableString *first = [@"Amin" mutableCopy];
NSMutableString *second = [@"Amin Negm" mutableCopy];
NSSet *set = [NSSet setWithObjects:first, second];

[first appendString:@" Negm"];

Both objects are equal than, but none is removed. (Which one?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...