• 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: NSNumber numberWithBool ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumber numberWithBool ?


  • Subject: Re: NSNumber numberWithBool ?
  • From: Rainer Brockerhoff <email@hidden>
  • Date: Mon, 26 Jan 2004 11:18:56 -0200

At 21:06 -0800 25/01/2004, email@hidden wrote:
>From: Glen Low <email@hidden>
>Date: Mon, 26 Jan 2004 12:59:54 +0800
>
>> Your code snippet will end up testing if such a dictionary value
>> exists. you want:
>>
>> if ([[tempDict valueForKey:@"status"] boolValue])
>>
> > // Daniel Currie
>
>That will be OK if you only need to check if the value is non-zero or
>not. I think boolValue tries to coerce the result to a BOOL, which
>means you can't tell whether the original number was an integer,
>something else or really a BOOL created with numberWithBool.

Here's a way to make NSDictionary implement nearly all of the same convenient methods that NSUserDefaults has:
(only stringArrayForKey: is missing)

@interface NSDictionary (ExtendedDictionary)

- (NSString*)stringForKey:(NSString*)key;
- (NSArray*)arrayForKey:(NSString*)key;
- (NSDictionary*)dictionaryForKey:(NSString*)key;
- (NSData*)dataForKey:(NSString*)key;
- (int)integerForKey:(NSString*)key;
- (float)floatForKey:(NSString*)key;
- (BOOL)boolForKey:(NSString*)key;

@end

@implementation NSDictionary (ExtendedDictionary)

- (NSString *)stringForKey:(NSString *)key {
id thing = [self objectForKey:key];
return [thing isKindOfClass:[NSString class]]?thing:nil;
}

- (NSArray *)arrayForKey:(NSString *)key {
id thing = [self objectForKey:key];
return [thing isKindOfClass:[NSArray class]]?thing:nil;
}

- (NSDictionary *)dictionaryForKey:(NSString *)key {
id thing = [self objectForKey:key];
return [thing isKindOfClass:[NSDictionary class]]?thing:nil;
}

- (NSData *)dataForKey:(NSString *)key {
id thing = [self objectForKey:key];
return [thing isKindOfClass:[NSData class]]?thing:nil;
}

- (int)integerForKey:(NSString *)key {
id thing = [self objectForKey:key];
if ([thing isKindOfClass:[NSString class]]) {
return [thing intValue];
} else if ([thing isKindOfClass:[NSNumber class]]) {
return [thing intValue];
}
return 0;
}

- (float)floatForKey:(NSString *)key {
id thing = [self objectForKey:key];
if ([thing isKindOfClass:[NSString class]]) {
return [thing floatValue];
} else if ([thing isKindOfClass:[NSNumber class]]) {
return [thing floatValue];
}
return 0.0;
}

- (BOOL)boolForKey:(NSString *)key {
id thing = [self objectForKey:key];
if ([thing isKindOfClass:[NSString class]]) {
if ([thing caseInsensitiveCompare:@"NO"]!=NSOrderedSame) {
return YES;
} else if ([thing intValue]) {
return YES;
}
} else if ([thing isKindOfClass:[NSNumber class]]) {
return [thing boolValue];
}
return NO;
}

@end

And this makes NSMutableDictionary set some common formats that are compatible for later inserting the dictionary into a .plist:

@interface NSMutableDictionary (ExtendedMutableDictionary)

- (void)setInteger:(int)value forKey:(NSString*)key;
- (void)setFloat:(float)value forKey:(NSString*)key;
- (void)setBool:(BOOL)value forKey:(NSString*)key;

@end

@implementation NSMutableDictionary (ExtendedMutableDictionary)

- (void)setInteger:(int)value forKey:(NSString *)key {
[self setObject:[NSString stringWithFormat:@"%d",value] forKey:key];
}

- (void)setFloat:(float)value forKey:(NSString *)key {
[self setObject:[NSString stringWithFormat:@"%f",value] forKey:key];
}

- (void)setBool:(BOOL)value forKey:(NSString *)key {
[self setObject:(value?@"YES":@"NO") forKey:key];
}

@end


--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"It's extremely unlucky to be superstitious, for no other reason
than it is always unlucky to be colossally stupid." (Stephen Fry)
Weblog: http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Memory Leak with NSMutable String
  • Next by Date: Re: Memory Leak with NSMutable String
  • Previous by thread: Re: NSNumber numberWithBool ?
  • Next by thread: ColorWell transparency
  • Index(es):
    • Date
    • Thread