gpt4 book ai didi

ios - 没有核心数据示例的Restkit

转载 作者:行者123 更新时间:2023-12-01 19:03:53 24 4
gpt4 key购买 nike

我设法用cocoapods安装RestKit,但是现在我对其包含的所有不同类感到困惑。

假设我上了课:

@interface Actor : NSObject

@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) NSNumber *age;

@end

并且网址“ http://SomeWebSite.com/actors”返回json:
{
Actors:
[
{
"firstName": "Will",
"lastName": "Ferrell",
"age": 25
},
{
"firstName": "Jim",
"lastName": "carrey",
"age": 25
}
]
}

有人可以举一个完整的例子,说明如何使用网址中的数据获取一组人吗?

非常感谢!

最佳答案

像这样:

    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];
}

关于ios - 没有核心数据示例的Restkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com