I have this problem. I am developing app in Swift and using RestKit to retrieve and post data back to API. However I have hit road block. If retrieved JSON payload contains some null value, the app wil crash with message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1994faba0'
What do I do? The mapped properties are Strings.
Mapped object:
public class User: NSObject {
public var id: Int? = 0
public var email: String?
public var firstName: String?
public var lastName: String?
public var name: String?
public var office: String?
public var phone: String?
public var company: String?
}
Mapping:
let userMapping = RKObjectMapping(forClass: User.self)
userMapping.addAttributeMappingsFromDictionary([
"id": "id",
"email": "email",
"first_name": "firstName",
"last_name": "lastName",
"name": "name",
"company": "company",
"phone": "phone",
"office": "office",
])
let responseDescriptor = RKResponseDescriptor(mapping: userMapping, pathPattern: currentUserPath, keyPath: "data", statusCodes: NSIndexSet(index: 200))
objectManager.addResponseDescriptor(responseDescriptor)
JSON response:
{
"status": "ok",
"data": {
"id": 1,
"email": "[email protected]",
"created_at": 1418832714451,
"updated_at": 1421077902126,
"admin": true,
"first_name": "John",
"last_name": "Doe",
"company": null,
"office": null,
"phone": null,
"name": "John Doe"
}
}
It crashes on each of these: office, phone and name.