【问题标题】:Restkit use object mapping without requestRestkit 无需请求即可使用对象映射
【发布时间】:2017-06-15 18:00:36
【问题描述】:

是否有任何方式使用 Restkit 框架将 json(nsdictionary)映射到对象而无需请求。

我有 json 并且需要将它映射到对象中

【问题讨论】:

  • 目前有代码吗?

标签: ios restkit


【解决方案1】:

您可以使用 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2014-02-01
    • 2013-05-24
    相关资源
    最近更新 更多