Adding Strings to NsMutableSets

牧云@^-^@ 提交于 2019-12-27 01:47:08

问题


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!


回答1:


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?)



来源:https://stackoverflow.com/questions/30535292/adding-strings-to-nsmutablesets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!