Boolean inside property list (Simple!) - iphone

I'm writing a small application right now and I got problems while reading out a property list...
My exact question is: How can I read out a boolean from the property list? Or better how can I read out this boolean from a NSDictionary?
Thanks, mavrick3.

The objects are stored as NSNumber objects, so to retrieve the BOOL you should use this method:
BOOL myBool = [someNSNumberObject boolValue];
To retrieve from a dictionary do something like this:
BOOl myBool = [[someDictionary objectForKey:#"someKey"] boolValue];
Documentation here: http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNumber/boolValue

Try storing the BOOL as a NSNumber ... then adding that to the Dictionary.
A simple example :
BOOL answered = YES;
NSNumber *answeredAsNumber = [NSNumber numberWithBool:answered];
[dict setObject:answeredAsNumber forKey:#"isAnswered"];
BOOL retrievedAnswered = [[dict objectForKey:#"isAnswered"] boolValue];

A BOOL should be stored as a NSNumber and you can access the BOOL value by saying
BOOL myvalue = [aNSNumber boolValue]

Related

How to wrap BOOL in NSValue

Having trouble wrapping a BOOL value in NSValue.
I tried this:
[NSValue valueWithPointer:[NSNumber numberWithBool:YES]]
An NSNumber is an NSValue subclass. As such,[NSNumber numberWithBool:YES] is already wrapping it in an NSValue for you.
Actually NSNumber is a heavier-weight descendant of NSValue. If you actually want to use an NSValue here's how you'd do that:
BOOL bool_value = YES;
[NSValue valueWithBytes:&bool_value objCType:#encode(BOOL)]
Here's some example code to expand on answer by Joshua Weinberg.
➤ To convert a primitive BOOL to a pseudo-Boolean object:
NSNumber* isWinning = [NSNumber numberWithBool:YES];
➤ To convert a pseudo-Boolean object to a primitive BOOL:
BOOL winner = [isWinning boolValue];
See NSNumber class reference.
Use an Objective-C literal:
#(YES)

Cannot figure out what simple thing I am doing wrong with this CoreData object

I have a CoreData object called "User". This object has a property declared with #property (nonatomic, retain) NSNumber * isloggedin; in User.h and in User.m I have #dynamic isloggedin;
In my AppDelegate I have this method:
- (NSManagedObject *) getLoggedInUserFromCurrentServer {
NSManagedObject *theUser = nil;
for (User *user in [[self myServer] users]) {
if (user.isloggedin == [NSNumber numberWithBool:YES]) {
// we found it
theUser = user;
break;
}
}
return theUser;
}
I have looked at the table in my database and there is only a single user and only a single server. That user has ZISLOGGEDIN set to 1.
When this code goes through, the IF statement says false.
[NSNumber numberWithBool:YES] returns 1 if I po it.
If I po user.isloggedin or po [user isloggedin] I get no member named isloggedin and Target does not respond to this message selector.
I had this code working before, but changed it and cannot figure out what I did before that made this work...or for that sake, why this won't work. I'm sure I'm missing some insanely obvious thing here...but I can't find it.
Simple: NSNumber is a pointer type and so by using == to compare, you're actually comparing pointers to distinct NSNumber objects. Thus, even if both contain the same BOOL values, your comparison won't evaluate to YES.
To compare their primitive BOOL types, use boolValue:
for (User *user in [[self myServer] users]) {
if ([user.isloggedin boolValue] == YES) {
// we found it
theUser = user;
break;
}
}

How to use BOOL and String

There is .plist file that has a key say XYZ and the value can be on or off.
Now I have this method BOOL isEnabled().
I want to check the value for that in that plist and return BOOL based on on or off.
How do I do that?
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:#"/var/mobile/Library/Preferences/com.apple.BTServer.airplane.plist"];
BOOL Location = [[plistDict objectForKey:#"airplaneMode"] boolValue];
return Location;
This code is for Location Toggle for iPhone/SBSettings. Here for Location toggle the values are stored as 1 and 0 but for Airplane mode for which I modifying this code the values are on/off.
How do I convert on /off to BOOL?
Is the on/off encoded as a string? If so, how about a string comparison?
NSString *strLocation = [[plistDict objectForKey:#"airplaneMode"] lowercaseString];
if ([strLocation isEqualToString:#"on"])
{
return TRUE;
}
return FALSE;
Yo probably want to use something like this to store a BOOL value on a dictionary/plist:
[myDictionary setObject:[NSNumber numberWithBool:YES]]
Though you'd use YES or NO depending on the value of the integer you want to store.
Or you can do a one-line if statement like so:
BOOL location = ([[plistDict objectForKey:#"airplaneMode"] isEqualToString:#"On"]) ? YES : NO;

How to convert and compare NSNumber to BOOL?

First I convert BOOL value to NSNumber in order to put it into NSUserDefaults. Later I would like to retrieve the BOOL value from the NSUserDefaults, but obviously I get NSNumber instead of BOOL. My questions are?
how to convert back from NSNumber to BOOL?
How to compare NSNumber to BOOL value.
Currently I have:
if (someNSNumberValue == [NSNumber numberWithBool:NO]) {
do something
}
any better way to to the comparison?
Thanks!
You currently compare two pointers. Use NSNumbers methods instead to actually compare the two:
if([someNSNumberValue isEqualToNumber:[NSNumber numberWithBool:NO]]) {
// ...
}
To get the bool value from a NSNumber use -(BOOL)boolValue:
BOOL b = [num boolValue];
With that the comparison would be easier to read for me this way:
if([num boolValue] == NO) {
// ...
}
Swift 4:
let newBoolValue = nsNumberValue.boolValue
NSUserDefaults has two methods to transparently operate with booleans:
- (BOOL)boolForKey:(NSString *)defaultName
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName
The ONLY way I managed to finagle a NSNumber's "booleanity" from its NSConcreteValue(doh!) was with the following...
id x = [self valueForKey:#"aBoolMaybe"];
if ([x respondsToSelector:#selector(boolValue)] &&
[x isKindOfClass:objc_getClass("__NSCFNumber")])
[self doSomethingThatExpectsABool:[x boolValue]];
Every other trick... FAILED. Buyer beware, this isn't foolproof (__NSCFNumber may well be platform/machine specific - it is solely Apple's implementation detail)... but as they say.. nothing else worked!

What's wrong with this method?

I'm getting a warning: "Return makes pointer from integer without a cast" for this method...
+(BOOL *)getBoolFromString:(NSString *)boolStr
{
if(boolStr == #"true" || boolStr == #"1"){
return YES;
}
return NO;
}
BOOL is not a class or object, so returning a pointer to a BOOL is not the same as returning a BOOL.
You should remove the * in +(BOOL *) and everything will be ok.
Besides what #Jasarien and #jlehr have said, you have a problem with this:
(boolStr == #"true" || boolStr == #"1")
That's doing pointer comparison, not object equality. You want:
([boolStr isEqualToString:#"true"] || [boolStr isEqualToString:#"1"])
To get a BOOL from an NSString, all you need to do is send a -boolValue message, like so:
NSString *myString = #"true"; // or #"YES", etc.
BOOL bool = [myString boolValue];