Re: Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method?
Re: Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method?
- Subject: Re: Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method?
- From: Derrick Bass <email@hidden>
- Date: Thu, 2 Feb 2006 10:22:05 -0600
On Feb 2, 2006, at 12:29 AM, email@hidden wrote:
All,
I am walking through a NSDictionary and have a variety of NSNumber
objects. Some where created with +numberWithBool: and others were
created with +numberWithInt:
Do I have any way to know whether that NSNumber represents a BOOL
rather than an int?
The following worked for me, but it relies on the fact that NSNumber
is actually a class-cluster. That's highly unlikely to change, but
still, it is relying on the internal details of how NSNumber is
implemented. Anyway, here goes:
@interface NSNumber (Introspection)
- (BOOL) isBool;
@end
@implementation NSNumber (Introspection)
- (BOOL) isBool {
static NSNumber *num = [[NSNumber alloc] initWithBool:NO]];
return [[self class] isEqualTo:[num class]] && !strcmp([self
objCType], @encode(BOOL));
}
@end
You can actually remove the strcmp. I put it there in case someday
Apple sees the light and decides to make BOOL into a real type (right
now it is just a typedef for char, which is why just comparing the
objCType won't work) and because I also wrote isInt, isChar,
isDouble, etc, etc, and I wanted to make all the routines look the same.
Derrick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden