4404 Views 6 Replies Latest reply: Oct 8, 2009 3:08 PM by jsalling
This is for information only for the benefit of anyone else who may have this problem or if someone has a better way to do it. I have found a way to get the job done. As stated before the type coming back from the NSManagedObject was a NSCFInteger type. I can cast that as a NSInteger. So I wrote the following code to see if I could get the BOOL value back from the NSManagedObject.
BOOL myBool = (NSInteger)[myManagedObject valueForKey:@"myAttribute"] == 1;
I figured I could do a simple compare to a literal 1 and retrieve a BOOL. This was not the case and the application crashed. Playing around with NSLog I noticed I could pull the NSInteger value into a NSString object so I tried this next:
BOOL myBool = [[NSString stringWithFormat@"%@", (NSInteger)[myManagedObject valueForKey:@"myAttribute"]]boolValue];
This too caused the application to crash. So I decided to combine the previous two ideas and came up with the following code:
BOOL myBool = [[NSString stringWithFormat@"%@", (NSInteger)[myManagedObject valueForKey:@"myAttribute"]]intValue] == 1;
It's not pretty but it gets the job done. I hope someone might know a better way to do this.
NSInteger is just a typedef. NSNumber is an actual class.
Thanks for the reply Q Lazarus. I agree with what you say.
The difficulty I see is the conversion I am using to convert the NSNumber (NSCFInteger) to a BOOL was complex and clunky. I was hoping that there was a slick way to get the job done that was more straightforward. I was also surprised that even though I was able to successfully import the NSNumber into the NSString, the boolValue method in the NSString object crashed. According to the documentation if the method found a "1" it would return 'YES' and if it found a "0" it would return a 'NO'. The intValue method was able to convert the "1" or "0" into an NSInteger or 1 or 0 and I was able to use that. I'm not sure what the difference is or if this could be some kind of bug.
Thank you for sticking with this, I did read the link and it explains what I understood the method would do. Just to make sure I've stated my problem correctly here is the process I went through to get working code.
My first attempt was to use the NSNumber object since it seemed an easy thing to use.
BOOL myBool = [[NSNumber initWithInteger:(NSInteger)[myManagedObject valueForKey:@"myKey"]]boolValue];
This crashed. From what I could tell the NSNumber was having problems initializing. So I noticed that I was able to log the value through NSLog and a formatted string. So I tried this
BOOL myBool = [[NSString initWithFormat:@"%@", (NSInteger)[myManagedObject valueForKey:@"myKey"]]boolValue];
This one crashed when the boolValue was run. So with a little more experimentation I came up with this that finally worked.
BOOL myBool = [[NSString initWithFormat:@"%@", (NSInteger)[myManagedObject valueForKey:@"myKey"]]intValue] == 1;
I believe the first try is what you were referring to in your first post. Do I have this correct or have I still missed your point?