Re: boolean into NSDictionary
Re: boolean into NSDictionary
- Subject: Re: boolean into NSDictionary
- From: Art Isbell <email@hidden>
- Date: Mon, 2 Jul 2001 15:59:12 -1000
On Monday, July 2, 2001, at 11:50 AM, Andreas Schweizer wrote:
NSNumber *boolNumber;
boolNumber = [myDictionary objectForKey:@"activeFlag"];
if ([boolNumber boolValue])
[myOtherDictionary setObject:[NSNumber numberWithBool:YES]
forKey:@"foundActiveObject"];
Because Objective-C may be a more dynamic and later-bound
language than many subscribers to this list are accustomed to,
defensive programming needs to be considered more carefully.
The compiler can't check certain things, so the programmer is
responsible for preventing runtime errors from leading to an app
crash.
In the above example, myDictionary can contain any type of
object as a value for "activeFlag". If that object doesn't
respond to boolValue, a crash may occur. Therefore, the
following alternative might be considered:
id boolNumber = [myDictionary objectForKey:@"activeFlag"];
// Assume that foundActiveObject will be set only if activeFlag was set
// to a NSNumber.
if ((boolNumber != nil) && [boolNumber isKindOfClass:[NSNumber class]])
[myOtherDictionary setObject:[NSNumber numberWithBool:YES]
forKey:@"foundActiveObject"];
Art Isbell
Apple iServices Technical Support
http://www.apple.com/iservices/webobjectssupport/
+1-808-591-0836