Hey. I am reading in a string from a file and attempting to use the resulting string to set a BOOL property on an object using the KVC method -setValue:forKeyPath:
Add a simple category to your project:
@implementation NSString (CharValue)
- (BOOL)charValue {
return [self isEqualToString:@"0"] ? NO : YES;
}
@end
When setting a BOOL
property using KVC, you need to pass an NSNumber
object. What you could do in your case is pass [NSNumber numberWithBool:[myString boolValue]]
. That should fix your crash.
I am catching the exception, checking it's name, and then retrying with a wrapped value when needed. Here is the code:
@try
{
[(NSObject*)retObj setValue:[[obj keyValuePairs] objectForKey:key]
forKeyPath:key];
}
@catch (NSException * e)
{
if ([[e name] isEqualToString:NSInvalidArgumentException])
{
NSNumber* boolVal = [NSNumber numberWithBool:[[[obj keyValuePairs] objectForKey:key] boolValue]];
[(NSObject*)retObj setValue:boolVal
forKeyPath:key];
}
}
Thanks anyway!