dynamic typecasting of managed object properties when doing setValuesForKeysWithDictionary - objective-c

I have a few NSManagedObject classes. I am pulling down some JSON data from a server which I parse into an NSDictionary object. When the conversion from JSON to NSDictionary occurs, all of my data is cast as NSStrings. When I then map this dictionary to my managedObject I get this:
Unacceptable type of value for attribute: property = "idexpert"; desired type = NSNumber; given type = __NSCFString; value = 1.'
So my managedobject is looking for an NSNumber but it's getting a string and throwing an exception
Is there a way that when I call setValuesForKeysWithDictionary I can automagically cast the values properly for the managedobject they are going into?
Thanks!

The best way to manage JSON attributes while saving in core data is to write a generic function that can override setValuesForKeysWithDictionary as per below:
#implementation NSManagedObject (safeSetValuesKeysWithDictionary)
- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter
{
NSDictionary *attributes = [[self entity] attributesByName];
for (NSString *attribute in attributes) {
id value = [keyedValues objectForKey:attribute];
if (value == nil) {
continue;
}
NSAttributeType attributeType = [[attributes objectForKey:attribute] attributeType];
if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) {
value = [value stringValue];
} else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) {
value = [NSNumber numberWithInteger:[value integerValue]];
} else if ((attributeType == NSFloatAttributeType) && ([value isKindOfClass:[NSString class]])) {
value = [NSNumber numberWithDouble:[value doubleValue]];
} else if ((attributeType == NSDateAttributeType) && ([value isKindOfClass:[NSString class]]) && (dateFormatter != nil)) {
value = [dateFormatter dateFromString:value];
}
[self setValue:value forKey:attribute];
}
}
#end
For more details refer this link here: http://www.cimgf.com/2011/06/02/saving-json-to-core-data/

If the json you are receiving actually has number values and they are getting cast as string you should get a new json parser. I recommend NXJson. Otherwise there wont be any magical casting happening.
If the json is returning strings such as {"idexpert":"1"} then you can override setValuesForKeysWithDictionary and do something like the code below;
-(void)setValuesForKeysWithDictionary:(NSDictionary *)d{
NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary:d];
NSString *value = [newDict valueForKey:#"idexpert"];
[newDict setValue:[NSNumber numberWithLong:[value longValue]] forKey:#"idexpert"];
[super setValuesForKeysWithDictionary:newDict];
}

Related

iOS Crash: " this class is not key value coding-compliant for the key url"

I am getting the following crash on crashlytics.
Fatal Exception: NSUnknownKeyException
[<__NSCFString 0x1742aeb80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.
This is where the crash occurred for some of the users.
id url = [[json[#"data"]valueForKey:#"value"]valueForKey:#"url"];
I'm not sure what is the best way to prevent this crash. I believe this is because json[#"data"] is an NSString in certain cases. So I believe I should check if this is an NSDictionary like this.
if ([json[#"data"] isKindOfClass:[NSDictionary class]]) {
id url = [[json[#"data"]valueForKey:#"value"]valueForKey:#"url"];
}
Any tips or suggestions are appreciated.
This is my end result after getting answers from here. Does this look okay? I didn't include all my code at first to keep things simple.
if ([json isKindOfClass:[NSDictionary class]]) {
id url = nil;
id type = nil;
NSDictionary *data = json[#"data"];
if ([data isKindOfClass:[NSDictionary class]]) {
type = data[#"type"];
NSDictionary *value = data[#"value"];
if ([value isKindOfClass:[NSArray class]]) {
url = [value valueForKey:#"url"];
}
if ([type isKindOfClass:[NSString class]] && [url isKindOfClass:[NSArray class]] && [url count] != 0) {
// do stuff
}
}
}
You should check NSDictionary one by one to prevent crash. Try my code below
NSDictionary *dictionary = json[#"data"];
NSString *output = #"";
if ([dictionary isKindOfClass:[NSDictionary class]]) {
dictionary = dictionary[#"value"];
if ([dictionary isKindOfClass:[NSDictionary class]]) {
output = dictionary[#"url"];
}
}
NSLog(#"%#", output);
You got crash because of calling valueForKey method on a NSString value. If someone says the reason for crash is call valueForKey when dictionary doesn't have this key, it's wrong. For more information Sending a message to nil in Objective-C
[dictionary isKindOfClass:[NSDictionary class]] always return NO if dictionary is nil so don't need to check dictionary in if statement. It's unnessary.
Your error means that json[#"data"]valueForKey:#"value"] doesn't NSDictionarry, so it have no #"url" key.
valueForKey it's KVC method, use objectForKey for dictionaries, and add more checks like:
id url = nil;
NSDictionary *data = [json objectForkey:#"data"];
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *value = [data objectForKey:#"value"];
if ([value isKindOfClass:[NSDictionary class]]) {
url = [value objectForKey:#"url"];
}
}

Dealing with different object types while iterating through an NSDictionary

for (NSArray *values in [serializedJSON allValues])
Sometimes the values in serializedJSON will be arrays, and sometimes they will be NSDictionaries. I would like to discriminate against one of them so I don't get any errors like I am now. So I only want the returning values in this case to be NSArrays, while in a second case I would only want them to be NSDictionaries.
Thanks in advanced!
If you need more info let me know
The standard, generic way to handle JSON is roughly as follows:
NSObject* jsonResult = [serializedJSON allValues];
if ([jsonResult isKindOfClass:[NSArray class]]) {
<handle NSArray>
}
else if ([jsonResult isKindOfClass:[NSDictionary class]]) {
<handle NSDictionary>
}
else if ([jsonResult isKindOfClass:[NSNumber class]]) {
<handle NSNumber>
}
else if ([jsonResult isKindOfClass:[NSString class]]) {
<handle NSString>
}
else if (jsonResult == [NSNull null]) {
<handle null>
}

Can I get AFNetworking to automatically parse NULL to nil?

We're using AFNetworking in our mobile app and a lot of times we will have JSON come back that has null for some values.
I'm getting tired of doing the following.
if ([json objectForKey:#"nickname"] isKindOfClass:[NSNull class]]) {
nickname = nil;
} else {
nickname = [json objectForKey:#"nickname"];
}
Anything we can do to make AFNetworking automagically set objects to nil or numbers to 0 if the value is null in the JSON response?
You can set flag setRemovesKeysWithNullValues to YES in AFHTTPSessionManager response serializer:
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:url sessionConfiguration:config];
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
[serializer setRemovesKeysWithNullValues:YES];
[manager setResponseSerializer:serializer];
It's not really possible, since the dictionary can't contain nil as the object for a key. The key would have to be left out entirely in order to get the behavior you'd want, which would be undesirable in its own way.
Suppose you didn't have control over the data you were receiving and didn't know what keys were present in the JSON. If you wanted to list them all, or display them in a table, and the keys for null objects were left out of the dictionary, you'd be seeing an incorrect list.
NSNull is the "nothing" placeholder for Cocoa collections, and that's why it's used in this case.
You could make your typing a bit easier with a macro:
#define nilOrJSONObjectForKey(JSON_, KEY_) [[JSON_ objectForKey:KEY_] isKindOfClass:[NSNull class]] ? nil : [JSON_ objectForKey:KEY_]
nickname = nilOrJSONObjectForKey(json, #"nickname");
DV_'s answer works great for AFHTTPSessionManager. But if you are using AFHTTPRequestOperation instead of the manager, try this:
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
serializer.removesKeysWithNullValues = YES;
op.responseSerializer = serializer;
There is one beautiful cocoapod called Minced https://github.com/hyperoslo/Minced that can do something that can help you handle NULL from JSON response. Instead of NULL it puts empty string.
If you replace the default NSJSONSerialization with SBJSON it will solve your problem.
SBJSON makes objects nil instead of NSJSONSerialization's choice of "null"
look at the requirements for the different JSON parsers you can use.
https://github.com/AFNetworking/AFNetworking#requirements
You can custom AFNetworking at this functions. set any value default to objects that is NULL
static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions) {
if ([JSONObject isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
for (id value in (NSArray *)JSONObject) {
[mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];
}
return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
} else if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:JSONObject];
for (id <NSCopying> key in [(NSDictionary *)JSONObject allKeys]) {
id value = (NSDictionary *)JSONObject[key];
if (!value || [value isEqual:[NSNull null]]) {
// custom code here
//[mutableDictionary removeObjectForKey:key];
[mutableDictionary setObject:#"" forKey:key];
} else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
mutableDictionary[key] = AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions);
}
}
return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
}
return JSONObject;
}

NSNull handling for NSManagedObject properties values

I'm setting values for properties of my NSManagedObject, these values are coming from a NSDictionary properly serialized from a JSON file. My problem is, that, when some value is [NSNull null], I can't assign directly to the property:
fight.winnerID = [dict objectForKey:#"winner"];
this throws a NSInvalidArgumentException
"winnerID"; desired type = NSString; given type = NSNull; value = <null>;
I could easily check the value for [NSNull null] and assign nil instead:
fight.winnerID = [dict objectForKey:#"winner"] == [NSNull null] ? nil : [dict objectForKey:#"winner"];
But I think this is not elegant and gets messy with lots of properties to set.
Also, this gets harder when dealing with NSNumber properties:
fight.round = [NSNumber numberWithUnsignedInteger:[[dict valueForKey:#"round"] unsignedIntegerValue]]
The NSInvalidArgumentException is now:
[NSNull unsignedIntegerValue]: unrecognized selector sent to instance
In this case I have to treat [dict valueForKey:#"round"] before making an NSUInteger value of it. And the one line solution is gone.
I tried making a #try #catch block, but as soon as the first value is caught, it jumps the whole #try block and the next properties are ignored.
Is there a better way to handle [NSNull null] or perhaps make this entirely different but easier?
It might be a little easier if you wrap this in a macro:
#define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; })
Then you can write things like
fight.winnerID = NULL_TO_NIL([dict objectForKey:#"winner"]);
Alternatively you can pre-process your dictionary and replace all NSNulls with nil before even trying to stuff it into your managed object.
Ok, I've just woke up this morning with a good solution. What about this:
Serialize the JSON using the option to receive Mutable Arrays and Dictionaries:
NSMutableDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:_receivedData options:NSJSONReadingMutableContainers error:&error];
...
Get a set of keys that have [NSNull null] values from the leafDict:
NSSet *nullSet = [leafDict keysOfEntriesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id key, id obj, BOOL *stop) {
return [obj isEqual:[NSNull null]] ? YES : NO;
}];
Remove the filtered properties from your Mutable leafDict:
[leafDict removeObjectsForKeys:[nullSet allObjects]];
Now when you call fight.winnerID = [dict objectForKey:#"winner"]; winnerID is automatically going to be (null) or nil as opposed to <null> or [NSNull null].
Not relative to this, but I also noticed that it is better to use a NSNumberFormatter when parsing strings to NSNumber, the way I was doing was getting integerValue from a nil string, this gives me an undesired NSNumber of 0, when I actually wanted it to be nil.
Before:
// when [leafDict valueForKey:#"round"] == nil
fight.round = [NSNumber numberWithInteger:[[leafDict valueForKey:#"round"] integerValue]]
// Result: fight.round = 0
After:
__autoreleasing NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
fight.round = [numberFormatter numberFromString:[leafDict valueForKey:#"round"]];
// Result: fight.round = nil
I wrote a couple of category methods to strip nulls from a JSON-generated dictionary or array prior to use:
#implementation NSMutableArray (StripNulls)
- (void)stripNullValues
{
for (int i = [self count] - 1; i >= 0; i--)
{
id value = [self objectAtIndex:i];
if (value == [NSNull null])
{
[self removeObjectAtIndex:i];
}
else if ([value isKindOfClass:[NSArray class]] ||
[value isKindOfClass:[NSDictionary class]])
{
if (![value respondsToSelector:#selector(setObject:forKey:)] &&
![value respondsToSelector:#selector(addObject:)])
{
value = [value mutableCopy];
[self replaceObjectAtIndex:i withObject:value];
}
[value stripNullValues];
}
}
}
#end
#implementation NSMutableDictionary (StripNulls)
- (void)stripNullValues
{
for (NSString *key in [self allKeys])
{
id value = [self objectForKey:key];
if (value == [NSNull null])
{
[self removeObjectForKey:key];
}
else if ([value isKindOfClass:[NSArray class]] ||
[value isKindOfClass:[NSDictionary class]])
{
if (![value respondsToSelector:#selector(setObject:forKey:)] &&
![value respondsToSelector:#selector(addObject:)])
{
value = [value mutableCopy];
[self setObject:value forKey:key];
}
[value stripNullValues];
}
}
}
#end
It would be nice if the standard JSON parsing libs had this behaviour by default - it's almost always preferable to omit null objects than to include them as NSNulls.
Another method is
-[NSObject setValuesForKeysWithDictionary:]
In this scenario you could do
[fight setValuesForKeysWithDictionary:dict];
In the header NSKeyValueCoding.h it defines that "Dictionary entries whose values are NSNull result in -setValue:nil forKey:key messages being sent to the receiver.
The only downside is you will have to transform any keys in the dictionary to keys that are in the receiver. i.e.
dict[#"winnerID"] = dict[#"winner"];
[dict removeObjectForKey:#"winner"];
I was stuck with the same problem, found this post, did it in a slightly different way.Using category only though -
Make a new category file for "NSDictionary" and add this one method -
#implementation NSDictionary (SuperExtras)
- (id)objectForKey_NoNSNULL:(id)aKey
{
id result = [self objectForKey:aKey];
if(result==[NSNull null])
{
return nil;
}
return result;
}
#end
Later on to use it in code, for properties that can have NSNULL in them just use it this way -
newUser.email = [loopdict objectForKey_NoNSNULL:#"email"];
Thats it

How to parse json specific fields using objective-c

I'm trying to parse a very simple json object with 1 value. (ocUnit test below)
- (void) testHatCartParseWithValidRefId {
NSString* data = #"{\"refid\":999}";
Cart* obj = [HatCartParseJson parseJsonAndReturnObject:data];
STAssertTrue([obj.refid isEqualToString:#"999"], #"fail");
}
In the implementation everything fails when I add the line to pull it from either key or index. How should I pull this from the json input? Please keep in mind I need this json parse (not string) the actual code I'm working with is a large set of JSON data.
+ (Cart *) parseJsonAndReturnObject:(NSString *)json
{
NSArray* cart = [json JSONValue];
for (NSDictionary* item in cart) {
Cart* obj = [[Cart alloc] init];
//NSString* refid = [item objectAtIndex:0];
//NSString* refid = [item objectForKey:#"refid"];
[obj setRefid:#"999"];
return obj;
}
return nil;
}
Thank you in advance
You are expecting that the return value of JSONValue is an NSArray, which in this case it isn't.
So, you must do a check if the return value is actually an NSArray, and if it is, then iterate through the collection, otherwise check if it's an NSDictionary, and if it is, then return the Cart object with the refid from the NSDictionary. If all of this fails, then just return nil.
As a side point, according to Apple's Object Ownership Policy, you should return autorelease-d objects from methods whose names do not contain the words "alloc", "new" or "copy". This would be one such method where you'd return an autorelease-d object.
+ (Cart *) parseJsonAndReturnObject:(NSString *)json
{
id cart = [json JSONValue];
NSString* refid = nil;
if([cart isKindOfClass:[NSArray class]]) {
refid = [[cart objectAtIndex:0] objectForKey:#"refid"];
} else if([cart isKindOfClass:[NSDictionary class]]) {
refid = [cart objectForKey:#"refid"];
}
if(refid) {
Cart* c = [[Cart alloc] init];
[c setRefid:refid];
return [c autorelease];
}
return nil;
}