我正在使用 RKXMLReaderSerialization 并尝试将 XML 响应从我的服务器映射到一个对象。它成功了,但是映射结果中的对象只有空值。
这是我试图从我的服务器映射的文本/xml 响应:
<Provision version="1.0">
<FileInfoWrapper>
<FileUrl>SomeFile.zip</FileUrl>
<FileName>SomeFile.zip</FileName>
<FileSha1>oi7NK/rFLL6dXqcu7ahaNfKsGkE=</FileSha1>
<FileSize>52220448</FileSize>
<Version>13</Version>
<Vital>true</Vital>
</FileInfoWrapper>
</Provision>
这是我的模型对象:
@interface FileInfoWrapper : NSObject
@property NSString *fileUrl;
@property NSString *fileName;
@property NSString *fileSha1;
@property long fileSize;
@property NSString *version;
@property BOOL vital;
@end
我添加了 RKXMLReaderSerialization:
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeTextXML];
[[self objectManager] setAcceptHeaderWithMIMEType:@"application/json,text/xml"];
我认为我正确设置了映射和响应描述符:
RKObjectMapping *fileInfoMapping = [RKObjectMapping mappingForClass:[FileInfoWrapper class]];
[fileInfoMapping addAttributeMappingsFromDictionary:@{
@"FileUrl": @"fileUrl",
@"FileName": @"fileName",
@"FileSha1": @"fileSha1",
@"FileSize": @"fileSize",
@"Version": @"version",
@"Vital": @"vital"}];
RKResponseDescriptor *fileInfoResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:fileInfoMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"Provision.FileInfoWrapper"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[self objectManager] addResponseDescriptor:fileInfoResponseDescriptor];
但是当我打电话时:
[[self objectManager] getObjectsAtPath:@"static/download/Installer.info.xml"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"****Success!****");
NSLog(@"mappingResult: %@", mappingResult);
FileInfoWrapper *fileInfo = [mappingResult firstObject];
NSLog(@"URL: %@", [fileInfo fileUrl]);
NSLog(@"Name: %@", [fileInfo fileName]);
NSLog(@"SHA1: %@", [fileInfo fileSha1]);
NSLog(@"Size: %lx", [fileInfo fileSize]);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"****Failure!****");
}];
所有的值都是空的:
I restkit.network:RKObjectRequestOperation.m:180 GET 'http://example.com/static/download/Installer.info.xml'
I restkit.network:RKObjectRequestOperation.m:250 GET 'http://example.com/static/download/Installer.info.xml' (200 OK / 1 objects) [request=0.1080s mapping=0.0060s total=0.1226s]
****Success!****
mappingResult: <RKMappingResult: 0x10064b190, results={
"Provision.FileInfoWrapper" = "<FileInfoWrapper: 0x10065f780>";
}>
URL: (null)
Name: (null)
SHA1: (null)
Size: 0
我究竟做错了什么?
更新:我打开了 RestKit/ObjectMapping 日志并得到了一些额外的信息:
D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: {
Provision = {
FileInfoWrapper = {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
}
and targetObject: (null)
T restkit.object_mapping:RKMapperOperation.m:320 Examining keyPath 'Provision.FileInfoWrapper' for mappable content...
D restkit.object_mapping:RKMapperOperation.m:300 Found mappable data at keyPath 'Provision.FileInfoWrapper': {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
}
D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
} with mapping <RKObjectMapping:0x10025e6b0 objectClass=FileInfoWrapper propertyMappings=(
"<RKAttributeMapping: 0x10025ebc0 FileSha1 => fileSha1>",
"<RKAttributeMapping: 0x10025ed90 FileName => fileName>"
…
)>
D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: <RKMappingOperation 0x10068f730> for 'FileInfoWrapper' object. Mapping values from object {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
} to object <FileInfoWrapper: 0x10068f290> with object mapping (null)
T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'FileUrl' to 'fileUrl'
T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'FileUrl'. Transforming from type '__NSDictionaryM' to 'NSString'
D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'FileInfoWrapper': {
fileName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = fileName;
};
fileSha1 = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = fileSha1;
};
…
}
T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'FileUrl to keyPath 'fileUrl' -- value is unchanged ((null))
T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'FileSha1' to 'fileSha1'
T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'FileSha1'. Transforming from type '__NSDictionaryM' to 'NSString'
T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'FileSha1 to keyPath 'fileSha1' -- value is unchanged ((null))
…