[英]Restkit without core data example
I managed to install RestKit with cocoapods but now I'm confused with all the different classes it contains. 我设法用cocoapods安装RestKit,但现在我对它包含的所有不同的类感到困惑。
Let's say that i got the class: 让我们说我上了课:
@interface Actor : NSObject
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) NSNumber *age;
@end
and that the url " http://SomeWebSite.com/actors " returns the json : 并且URL“ http://SomeWebSite.com/actors ”返回json:
{
Actors:
[
{
"firstName": "Will",
"lastName": "Ferrell",
"age": 25
},
{
"firstName": "Jim",
"lastName": "carrey",
"age": 25
}
]
}
Can someone please give a complete example of how to get an array of persons with the data from the url? 有人可以提供一个完整的例子,说明如何从网址获取数据的人数?
Thanks alot! 非常感谢!
Something like this: 像这样的东西:
RKObjectMapping* actorMapping = [RKObjectMapping mappingForClass:[Actor class]];
[actorMapping addAttributeMappingsFromDictionary:@{
@"firstName": @"firstName",
@"lastName": @"lastName",
@"age": @"age",
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:actorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"Actors" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:@"http://SomeWebSite.com/actors"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Actors: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.