Reputation: 133
When receiving an answer from my server which contains null values (in the JSON response), RestKit internal mapping crashes when dealing with the null value.
is there any way to overcome this?
I have tried extending NSNull to include the "missing" methods but this did not work and seems wrong.
following is my mapping and object code and the restkit output:
object mapping:
var tempmapping = RKObjectMapping(forClass: PRLAUser.classForCoder())
tempmapping.addAttributeMappingsFromDictionary([
"firstName" : "firstName",
"secondName" : "lastName",
"email" : "email",
"id" : "prlaToken"
])
object code - well at least the variables that are being mapped:
var email : String?
var firstName : String?
var lastName : String?
var prlaToken : String?
crash log:
Parola[6138:3119821] -[NSNull length]: unrecognized selector sent to instance 0x197b86ba0
2015-01-20 15:53:37.973 Parola[6138:3119821] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x197b86ba0' * First throw call stack: (0x1867fa59c 0x196f040e4 0x186801664 0x1867fe418 0x186702b6c 0x100707964 0x100023dc8 0x187605b10 0x1000a34a0 0x1000a4078 0x1000a99d4 0x1000a8dfc 0x10009a8f4 0x1000995a8 0x10009bbb4 0x10009c294 0x10009ca84 0x1875f461c 0x1000e4754 0x1000e2acc 0x1875f461c 0x1876b626c 0x1007b4df0 0x1007bf854 0x1007b8120 0x1007c175c 0x1007c2f18 0x1977252e4 0x197724fa8) libc++abi.dylib: terminating with uncaught exception of type NSException
Would very much appreciate any assistance on the matter.
Upvotes: 2
Views: 1445
Reputation: 416
version 0.23.1 would work for you. I posted issue on RestKit github page also with temporary fix.
https://github.com/RestKit/RestKit/issues/2155
In an object that is getting NSNull into String I did something like this
public override func validateValue(ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKeyPath inKeyPath: String, error outError: NSErrorPointer) -> Bool {
if let memory = ioValue.memory {
if inKeyPath == "hint" && ioValue.memory is NSNull {
ioValue.memory = nil
return true
}
}
return super.validateValue(ioValue, forKeyPath: inKeyPath, error: outError)
}
You can also change RKMappingOperationDataSource
to return in delegate NO when asking for
- (BOOL)mappingOperationShouldSetUnchangedValues:(RKMappingOperation *)mappingOperation;
BTW. This question already exists on Stack: RestKit JSON null value crash
Upvotes: 2
Reputation: 52538
If the server sends you a null value, you first have to decide what that really means. For example, if you expect a string in a dictionary, then the key might not be present, the value might be null, the value might be an empty string, and you need to decide whether you want to treat these cases the same or not. Whoever designed the JSON data might actually want you to treat null values differently.
After that, the straightforward thing is to either check for null values by comparing if (object == (id) [NSNull null]) or by adding a category to NSDictionary which does the checks for you and returns whatever value you want.
Upvotes: 0