Re: -[NSSet containsObject:] returns NO when it should return YES
Re: -[NSSet containsObject:] returns NO when it should return YES
- Subject: Re: -[NSSet containsObject:] returns NO when it should return YES
- From: Roland King <email@hidden>
- Date: Sun, 20 Feb 2011 14:03:46 +0800
On 20-Feb-2011, at 1:54 PM, Andy Lee wrote:
>
> One thing I'm puzzled about is that NSSet does not copy its entries the way NSDictionary copies its keys. This is implied by the member: documentation quoted earlier:
>
> "If the set contains an object equal to object (as determined by isEqual:) then that object (***typically this will be object***), otherwise nil."
>
> A quick experiment confirms that elements are not copied, which leads to the risk that NSDictionary avoids. If elements of the set happen to be mutable, one element could mutate in such a way as to be isEqual: to another. This can be demonstrated actually happening:
>
No experiment needed - it's in the documentation .. quoting ..
"The objects in a set must respond to the NSObject protocol methods hash and isEqual: (see NSObject for more information). If mutable objects are stored in a set, either the hash method of the objects shouldn’t depend on the internal state of the mutable objects or the mutable objects shouldn’t be modified while they’re in the set. For example, a mutable dictionary can be put in a set, but you must not change it while it is in there. (Note that it can be difficult to know whether or not a given object is in a collection)."
"NSSet provides a number of initializer methods, such as setWithObjects: and initWithArray:, that return an NSSet object containing the elements (if any) you pass in as arguments. Objects added to a set are not copied (unless you pass YES as the argument to initWithSet:copyItems:). Rather, an object is added directly to a set. In a managed memory environment, an object receives a retain message when it’s added; in a garbage collected environment, it is strongly referenced. For more information on copying and memory management, see “Copying Collections.”"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden