Re: how do I put boolean variables in a NSDictionary?
Re: how do I put boolean variables in a NSDictionary?
- Subject: Re: how do I put boolean variables in a NSDictionary?
- From: James Bucanek <email@hidden>
- Date: Mon, 19 Jun 2006 13:09:07 -0700
Alan Smith wrote on Monday, June 19, 2006:
>Hi all,
>I want to put a bool in a NSDictionary to save in the NSUserDefaults.
>I've tried using this:
>
>NSDictionary *dict = [NSDictionary
>dictionaryWithObjectsAndKeys:@"Cheat", @"type", YES, @"compilation",
>nil];
>
>I get a SIGBUS error. It's because of the bool I know. I tried:
That's correct, because all of the parameters to dictionaryWithObjectsAndKeys must be object references. A BOOL is not an object reference, so you are passing an BOOL value as a pointer.
>NSDictionary *dict = [NSDictionary
>dictionaryWithObjectsAndKeys:@"Cheat", @"type", [NSNumber
>numberWithInt: 0], @"compilation", nil];
[NSNumber numberWithBool:NO] is more readable.
>And then:
>
>if ([[dict valueForKey: @"compilation"] boolValue])
>
>and that goes through. But that isn't as pretty or understandable as
>it could be.
That's as good as it's going to get. All primative type (int, char, BOOL, ...) must be wrapped in NSObjects to exist in a dictionary and then unwrapped again when you get them out agian.
--
James Bucanek
_______________________________________________
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