• 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: boolean into NSDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: boolean into NSDictionary (From: Andreas Schweizer <email@hidden>)

  • Prev by Date: Re: Getting the user's name from cocoa
  • Next by Date: Re: NSOpenPanel obsolete method?
  • Previous by thread: Re: boolean into NSDictionary
  • Next by thread: Re: boolean into NSDictionary
  • Index(es):
    • Date
    • Thread