我是使用RestKit的新手,但我根本无法理解它是如何工作的...... 拜托,能在某处解释一下吗?
我的Json文件是:
{
"colors":
{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
"cyan":"#0ff",
"magenta":"#f0f",
"yellow":"#ff0",
"black":"#000"
}
}
我托管此文件的路径是:http://186.36.181.116/tesis/file.json
我在ViewDidLoad方法中尝试的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[colores class]];
[mapping addAttributeMappingsFromArray:@[@"colors"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:@"/tesis/:coloresID" keyPath:@"colors" statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://186.36.181.116/tesis/file.json"]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
colores *colores = [result firstObject];
NSLog(@"Mapped the article: %@", colores);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
[operation start];
}
我的班级“colores”如下:
#import <Foundation/Foundation.h>
@interface colores : NSObject{}
@property (weak, nonatomic) IBOutlet NSString *colores;
@end
提前非常感谢!!