RestKit JSON null value crash

我是研究僧i 提交于 2020-01-02 05:23:06

问题


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": "some@email.com",
    "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.


回答1:


Try to use another version of RestKit. There were changes in RestKit codebase regarding the null value bindings.




回答2:


@Hotlicks is right, nulls should be handled by the client. I believe the reason for this crash is that Restkit cannot properly introspect class property types in Swift. We solved this in our project by adding explicit targetClass to our RKObjectMapping. So, instead of using the addAttributeMappingsFromDictionary method, try:

let userMapping = RKObjectMapping(forClass: User.self)
let simpleMapping = RKAttributeMapping(fromKeyPath: "first_name", toKeyPath: "firstName")
simpleMapping.propertyValueClass = NSString.classForCoder() // we used class for coder when we did our project, but you might have success with something else.
userMapping.addPropertyMapping(simpleMapping)

You have to do the same for relationships as well, like so:

let relationshipMapping = RKRelationshipMapping(fromKeyPath: "person", toKeyPath: "person", withMapping: Person.self)
relationshipMapping.propertyValueClass = Person.classForCoder()
userMapping.addPropertyMapping(relationshipMapping)

This allows an automatic conversion from NSNull to an Swift optional.



来源:https://stackoverflow.com/questions/27917104/restkit-json-null-value-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!