Re: How tell tell if an NSNumber was initialized from a float or int?
Re: How tell tell if an NSNumber was initialized from a float or int?
- Subject: Re: How tell tell if an NSNumber was initialized from a float or int?
- From: "email@hidden" <email@hidden>
- Date: Sat, 26 Jun 2010 20:45:43 +0100
On 26 Jun 2010, at 19:51, Quincey Morris wrote:
> On Jun 26, 2010, at 10:52, Kyle Sluder wrote:
>
>> On Jun 26, 2010, at 9:45 AM, Jaime Magiera <email@hidden> wrote:
>>
>>> Hello,
>>>
>>> The list search keeps timing out for me, and web searches are not finding anything. I can see how to tell if an NSNumber was initialized with an int or boolean (NSCFNumber vs. NSCFBoolean). However, I can't figure out how to determine if the NSNumber was initialized with an int or float.
>>
>> I hope you don't mind my asking why you need to do this? The existence of NSCFNumber and NSCFBoolean is an implementation detail upon which you can't rely (well, probably more like shouldn't).
>
> "Can't" is more correct than "shouldn't", in this context. The NSNumber documentation for 'objCType' explicitly says:
>
>> Special Considerations
>> The returned type does not necessarily match the method the receiver was created with.
>
> Incidentally, the OP is wrong in claiming that it's possible "to tell if an NSNumber was initialized with an int or boolean (NSCFNumber vs. NSCFBoolean)". This is triply wrong:
>
> 1. Unless this has changed recently, [NSNumber numberWithInt: 1] and [NSNumber numberWithBool: YES] both return the same (singleton) object, as do [NSNumber numberWithInt: 0] and [NSNumber numberWithBool: NO].
>
> 2. Testing against NSCF classes is a mistake, because they're private and undocumented, AFAIK.
>
> 3. As with 'objCType', the best you can determine is what type has been chosen to *represent* the number, not what what type was passed when creating the number.
>
> In other words, NSNumber is a wrapper of numeric values, not a wrapper of C scalar types.
>
In light of the above a 10.5 + GC solution might be:
1. Define a category on NSNumber.
2. In the category file allocate a static map table myMap = [NSMapTable mapTableWithWeakToStrongObjects];
3. Define counterparts to each factory method: eg: myNumberWithBool:(BOOL)theBool, etc
4. Each factory method allocates an instance and performs the equivalent of [myMap setObject:@"BOOL" forKey:[NSNumber numberWithBool:theBool]]
5. The initialisation type for any NSNumber instance can be obtained by querying the map table [myMap objectForKey:someNumber];
Regards
Jonathan Mitchell
Developer
Mugginsoft LLP
http://www.mugginsoft.com_______________________________________________
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