• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method? (From: email@hidden)

  • Prev by Date: Re: drawing/graphing package
  • Next by Date: Re: Objective-C or Objective-C++
  • Previous by thread: Re: Do I know whether a NSNumber was created with +numberWithBool rather than a different +numberWithxx method?
  • Next by thread: Using Your Own Data Format in Core Data
  • Index(es):
    • Date
    • Thread