Restkit使用没有请求的对象映射

时间:2017-01-30 14:26:20

标签: ios restkit

是否可以使用Restkit框架将json(nsdictionary)映射到没有请求的对象。

我有json,需要将它映射到对象

1 个答案:

答案 0 :(得分:0)

您可以使用RKMappingOperation类。这是文档中的示例:

@interface RKMetadataExample : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSURL *URL;
@property (nonatomic, copy) NSDate *mappedAt;
@end

RKMetadataExample *example = [RKMetadataExample new];
NSDictionary *representation = @{ @"name": @"Blake Watters" };
NSDictionary *metadata = @{ @"URL": [NSURL URLWithString:@"http://restkit.org"] };

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[RKMetadataExample class]];
[objectMapping addAttributeMappingsFromDictionary:@{ @"name": @"name", @"@metadata.URL": @"URL" }];

RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:representation destinationObject:example mapping:objectMapping];
mappingOperation.metadata = metadata;

NSError *error = nil;
BOOL success = [mappingOperation execute:&error];