【发布时间】:2017-06-15 18:00:36
【问题描述】:
是否有任何方式使用 Restkit 框架将 json(nsdictionary)映射到对象而无需请求。
我有 json 并且需要将它映射到对象中
【问题讨论】:
-
目前有代码吗?
是否有任何方式使用 Restkit 框架将 json(nsdictionary)映射到对象而无需请求。
我有 json 并且需要将它映射到对象中
【问题讨论】:
您可以使用 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];
【讨论】: