Is it possible to have 2 different NSNumber objects with the same value?
I wanted to have a list or set which might contain two NSNumbers with the same integer value but it seems开发者_开发技巧 that memory is optimised so they are the same object.
e.gNSNumber* n1=[NSNumber numberWithInt:10];
NSNumber* n2=[NSNumber numberWithInt:10];
then n1==n2;
Is there a way round this so that n1!=n2 ?
Not really. Cocoa keeps a cache of small numbers (IIRC those that represents integers from 0 to 12), and tagged pointers will also prevent that.
If you really need that, one option is to create a class that boxes NSNumber
instances. In this way you can guarantee that different instances of your class will have different addresses.
精彩评论