6 Replies Latest reply: Oct 8, 2009 3:08 PM by jsalling
Cycles4Fun Level 1 (5 points)
I am attempting to retrieve a BOOL from a NSManagedObject. The entity type is boolean and is bound to an NSButton type. The NSButton is able to get and set the BOOL value. The application is able to save and read the BOOL value accurately from the persisted store. However when I attempt to read the value from the NSManagedObject using the valueForKey method I receive an EXCBADACCESS error.

I have done some research and I found this code on a site:

[[NSNumber numberWithInt:((NSInteger)[managedObject valueForKey:@"fieldName"])] boolValue];

This code did not correct the problem. When I checked the class type "NSCFNumber" is returned.

I am not sure how to work with this type. Is there some way to convert this type to a BOOL? Any help will be greatly appreciated.

Thanks in advance.

MacBook Pro 15.4, iMac 20, iPhone 3G, Mac OS X (10.5.8), XCode 3.1
  • Cycles4Fun Level 1 (5 points)
    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.
  • Cycles4Fun Level 1 (5 points)
    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.
  • Q Lazarus Level 1 (80 points)
    It doesn't sound like you actually clicked the link or read any of the NSNumber documentation.
  • Cycles4Fun Level 1 (5 points)
    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?
  • jsalling Level 1 (0 points)
    You're thinking it's complex, because you're making it complex. How about this?

    BOOL myBool = [[myManagedObject valueForKey:@"myKey"] boolValue];