iOS 和 Swift 使用 RestKit 的映射属性的空值

null values for mapped attributes using RestKit for iOS and Swift

我正在尝试使用 JSON 数据执行 POST 请求到我的 API:

我的class实现:

class Person: NSObject {
    var theId: String?
    var countrycode: Int?
    var language: String?
}

应用委托中的映射和描述符class:

let requestMapping = RKObjectMapping.request()
requestMapping?.addAttributeMappings(from: ["theId":"theId", "countrycode":"countrycode", "language":"language"])

let requestDescriptor = RKRequestDescriptor(mapping: requestMapping, objectClass: Person.self, rootKeyPath: nil, method: .any)

objectManager.addRequestDescriptor(requestDescriptor)

我在下面的代码中使用它:

self.objectManager.post(person, path: "my_path", parameters: nil, success: { (operation, result) in
    // OK Response Here
}) { (operation, error) in
    // Error Response Here
}

我的 API 预计 JSON:

{
    "theId":"123",
    "countrycode":1001,
    "language":"en"
}

但是我所有的映射值都设置为空。但是在调试器中,我的对象值被设置为正确的值,我在使用该对象之前确认了这一点。

这是我的日志:

2019-04-18 13:22:44.382556+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:1178 Performing mapping operation: <RKMappingOperation 0x600002ba9ae0> for '__NSDictionaryM' object. Mapping values from object <AE_Project_iOS.Person: 0x600000094540> ((null)) to object {
} with object mapping (null)
2019-04-18 13:22:44.382707+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'theId' to 'theId'
2019-04-18 13:22:44.382801+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'theId' to 'theId'. Value: (null)
2019-04-18 13:22:44.382874+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.382972+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'countrycode' to 'countrycode'
2019-04-18 13:22:44.383054+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'countrycode' to 'countrycode'. Value: (null)
2019-04-18 13:22:44.383129+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.383223+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'language' to 'language'
2019-04-18 13:22:44.388335+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'language' to 'language'. Value: (null)
2019-04-18 13:22:44.388464+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.388562+0300 AE-Project-iOS[70642:6658348] D restkit.object_mapping:RKMappingOperation.m:1277 Finished mapping operation successfully...

这是完整的日志:

2019-04-18 13:22:44.382334+0300 AE-Project-iOS[70642:6658348] D restkit.object_mapping:RKMappingOperation.m:1177 Starting mapping operation...
2019-04-18 13:22:44.382556+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:1178 Performing mapping operation: <RKMappingOperation 0x600002ba9ae0> for '__NSDictionaryM' object. Mapping values from object <AE_Project_iOS.Person: 0x600000094540> ((null)) to object {
} with object mapping (null)
2019-04-18 13:22:44.382707+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'theId' to 'theId'
2019-04-18 13:22:44.382801+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'theId' to 'theId'. Value: (null)
2019-04-18 13:22:44.382874+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.382972+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'countrycode' to 'countrycode'
2019-04-18 13:22:44.383054+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'countrycode' to 'countrycode'. Value: (null)
2019-04-18 13:22:44.383129+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.383223+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'language' to 'language'
2019-04-18 13:22:44.388335+0300 AE-Project-iOS[70642:6658348] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'language' to 'language'. Value: (null)
2019-04-18 13:22:44.388464+0300 AE-Project-iOS[70642:6658348] D restkit.network:RKObjectParameterization.m:132 Serialized (null) value at keyPath to NSNull ((null))
2019-04-18 13:22:44.388562+0300 AE-Project-iOS[70642:6658348] D restkit.object_mapping:RKMappingOperation.m:1277 Finished mapping operation successfully...
2019-04-18 13:22:44.389680+0300 AE-Project-iOS[70642:6670969] I restkit.network:RKObjectRequestOperation.m:134 POST 'http://example.com/ProjectRestServices/jaxrs/person'
2019-04-18 13:22:44.389814+0300 AE-Project-iOS[70642:6670969] D restkit.network:RKObjectRequestOperation.m:135 request.headers={
    "Accept-Language" = "en;q=1, ar-US;q=0.9";
    "Content-Type" = "application/json; charset=utf-8";
}
2019-04-18 13:22:44.389916+0300 AE-Project-iOS[70642:6670969] T restkit.network:RKObjectRequestOperation.m:137 request.body={"person":{"countrycode":null,"language":null,"theId":null}}
2019-04-18 13:22:44.412168+0300 AE-Project-iOS[70642:6670345] I restkit.network:RKResponseMapperOperation.m:428 Non-successful status code encountered: performing mapping with nil target object.
2019-04-18 13:22:44.412305+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMapperOperation.m:414 Executing mapping operation for representation: {
    responseCode = 10000;
    responseString = "Entered ID is incorrect. Please enter a correct ID number";
    responseStringAR = "Entered ID is incorrect. Please enter a correct ID number";
}
 and targetObject: (null)
2019-04-18 13:22:44.412425+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMapperOperation.m:357 Examining keyPath '' for mappable content...
2019-04-18 13:22:44.412605+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMapperOperation.m:337 Found mappable data at keyPath '': {
    responseCode = 10000;
    responseString = "Entered ID is incorrect. Please enter a correct ID number";
    responseStringAR = "Entered ID is incorrect. Please enter a correct ID number";
}
2019-04-18 13:22:44.412790+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMapperOperation.m:258 Asked to map source object {
    responseCode = 10000;
    responseString = "Entered ID is incorrect. Please enter a correct ID number";
    responseStringAR = "Entered ID is incorrect. Please enter a correct ID number";
} with mapping <RKObjectMapping:0x6000031b0460 objectClass=AE_Project_iOS.ErrorMessage propertyMappings=(
    "<RKAttributeMapping: 0x6000014cb2d0 responseString => message>",
    "<RKAttributeMapping: 0x6000014ff510 responseStringAR => messageAr>",
    "<RKAttributeMapping: 0x6000014ff630 responseCode => code>"
)>
2019-04-18 13:22:44.413105+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMappingOperation.m:1177 Starting mapping operation...
2019-04-18 13:22:44.413383+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:1178 Performing mapping operation: <RKMappingOperation 0x600002ba7660> for 'AE_Project_iOS.ErrorMessage' object. Mapping values from object {
    responseCode = 10000;
    responseString = "Entered ID is incorrect. Please enter a correct ID number";
    responseStringAR = "Entered ID is incorrect. Please enter a correct ID number";
} to object <AE_Project_iOS.ErrorMessage: 0x600001402c40> with object mapping (null)
2019-04-18 13:22:44.413624+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:698 Found transformable value at keyPath 'responseString'. Transforming from class '__NSCFString' to 'NSString'
2019-04-18 13:22:44.413828+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'responseString' to 'message'
2019-04-18 13:22:44.413966+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'responseString' to 'message'. Value: Entered ID is incorrect. Please enter a correct ID number
2019-04-18 13:22:44.414078+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:698 Found transformable value at keyPath 'responseStringAR'. Transforming from class '__NSCFString' to 'NSString'
2019-04-18 13:22:44.414206+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'responseStringAR' to 'messageAr'
2019-04-18 13:22:44.414365+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'responseStringAR' to 'messageAr'. Value: Entered ID is incorrect. Please enter a correct ID number
2019-04-18 13:22:44.414485+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:698 Found transformable value at keyPath 'responseCode'. Transforming from class 'NSTaggedPointerString' to 'NSNumber'
2019-04-18 13:22:44.414605+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:717 Mapping attribute value keyPath 'responseCode' to 'code'
2019-04-18 13:22:44.414723+0300 AE-Project-iOS[70642:6670345] T restkit.object_mapping:RKMappingOperation.m:733 Mapped attribute value from keyPath 'responseCode' to 'code'. Value: 10000
2019-04-18 13:22:44.414891+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMappingOperation.m:1277 Finished mapping operation successfully...
2019-04-18 13:22:44.415039+0300 AE-Project-iOS[70642:6670345] D restkit.object_mapping:RKMapperOperation.m:440 Finished performing object mapping. Results: {
    "" = "<AE_Project_iOS.ErrorMessage: 0x600001402c40>";
}
2019-04-18 13:22:44.415461+0300 AE-Project-iOS[70642:6670345] E restkit.network:RKObjectRequestOperation.m:172 POST 'http://example.com/ProjectRestServices/jaxrs/person' (406 Not Acceptable / 0 objects) [request=0.0214s mapping=0.0000s total=0.0256s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "<AE_Project_iOS.ErrorMessage: 0x600001402c40>" UserInfo={RKObjectMapperErrorObjectsKey=(
    "<AE_Project_iOS.ErrorMessage: 0x600001402c40>"
), NSLocalizedDescription=<AE_Project_iOS.ErrorMessage: 0x600001402c40>}
2019-04-18 13:22:44.415581+0300 AE-Project-iOS[70642:6670345] T restkit.network:RKObjectRequestOperation.m:179 response.body={"responseCode":"10000","responseString":"Entered ID is incorrect. Please enter a correct ID number","responseStringAR":"Entered ID is incorrect. Please enter a correct ID number"}

RestKit 使用 key/value 编码从对象中提取属性。这意味着必须声明 Swift 对象中的属性 @objc 并且可能 dynamic 以便 RestKit 发现它们。