我想在这里问点什么。我已映射该对象并将其添加到RKResponseDescriptor并且它可以工作,但这里存在问题。当来自JSON的数据不同时,它不会映射到已经创建的对象我只是意识到它没有很好的映射。
// Setup our object mappings
RKObjectMapping *applicationMapping = [RKObjectMapping mappingForClass:[ApplicationSettings class]];
[applicationMapping addAttributeMappingsFromDictionary:@{
@"filterRead" : @"filterRead",
@"filterUserComments" : @"filterUserComments"
}];
RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[Category class]];
[categoryMapping addAttributeMappingsFromDictionary:@{
@"id" : @"categoryID",
@"title" : @"title",
@"slug" : @"slug"
}];
RKObjectMapping *commentMapping = [RKObjectMapping mappingForClass:[Comment class]];
commentMapping.forceCollectionMapping = YES;
[commentMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"comments"];
[categoryMapping addAttributeMappingsFromDictionary:@{
@"(coments).id" : @"commentID",
@"(comments).date" : @"createdDate",
@"(comments).content" : @"comment"
}];
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[Error class]];
[errorMapping addAttributeMappingsFromDictionary:@{
@"error" : @"error",
@"status" : @"status"
}];
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[Post class]];
postMapping.forceCollectionMapping = YES;
[postMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"posts"];
[errorMapping addAttributeMappingsFromDictionary:@{
@"id" : @"postID",
@"title" : @"title",
@"content" : @"content",
@"date" : @"createdDate",
@"modified" : @"modifiedDate"
}];
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping addAttributeMappingsFromDictionary:@{
@"nonce" : @"nonce",
@"cookie" : @"cookie",
@"username" : @"userName",
@"email" : @"email"
}];
// Register our mappings with the provider using a response descriptor
RKResponseDescriptor *userResponse = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
RKResponseDescriptor *applicationResponse = [RKResponseDescriptor responseDescriptorWithMapping:applicationMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
RKResponseDescriptor *categoryResponse = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
RKResponseDescriptor *commentResponse = [RKResponseDescriptor responseDescriptorWithMapping:commentMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
RKResponseDescriptor *errorResponse = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
RKResponseDescriptor *postResponse = [RKResponseDescriptor responseDescriptorWithMapping:postMapping
pathPattern:nil
keyPath:nil
statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];
[objectManager addResponseDescriptorsFromArray:
@[
applicationResponse,
categoryResponse,
commentResponse,
userResponse,
postResponse,
errorResponse
]] ;
编辑1:
[objectManager addResponseDescriptorsFromArray:
@[
applicationResponse,
categoryResponse,
commentResponse,
errorResponse,
postResponse,
userResponse
]] ;
对于这个,可以映射用户的JSON数据,但不能映射到另一个。我的意思是我们可以将对象映射到这样的东西吗?因为我改变了responseDescriptors的顺序,它只是在底部映射了对象。
抱歉我的英语不好
答案 0 :(得分:0)
您的问题似乎是您没有设置任何描述符的pathPattern
或keyPath
。这意味着每当RestKit尝试处理任何JSON时,它将尝试处理每个响应描述符,因为它们总是匹配。所以你会得到一些好的对象和一些空对象(创建但没有数据集)。
您需要教授响应描述符哪些数据适合映射。
如果每种类型的内容都有不同的网址:
##_URL_##/users.json
##_URL_##/categories.json
为每个描述符设置pathPattern
:
userResponse:
...ithMapping:userMapping
pathPattern:@"users.json"
keyPath:nil ...
如果您在JSON中有不同的内容部分,则需要使用keyPath
。
尽可能多地向pathPattern
和keyPath
添加信息,以区分并允许RestKit了解您希望为每个请求获得的数据。