【问题标题】:How to make a RequestDescriptor within array attribute (iOS RestKit)如何在数组属性中创建 RequestDescriptor (iOS RestKit)
【发布时间】:2016-01-25 21:48:34
【问题描述】:

这里是api请求定义(json格式示例):

{"Name":"my name", "Location":[{"Lon":123.4563, "Lat":22.3333},{"Lon":133.1553, "Lat":12.2111}]}

这是object-c中的两个类:

@interface MyLocation : NSObject
@property (nonatomic, strong) NSNumber* lat;
@property (nonatomic, strong) NSNumber* lon;
@end

@interface MyRequest : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) NSMutableArray<MyLocation*> *locations;
@end

我想做一个类似的请求描述符

// MyLocation attribute mapping
RKObjectMapping* attrMapping = [RKObjectMapping mappingForClass:[MyLocation class]];
[attrMapping addAttributeMappingsFromDictionary:@{@"Lon":@"lon", @"Lat":@"lat"}];
RKRelationshipMapping *relatedMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Location" toKeyPath:@"locations" withMapping:attrMapping];

// inverse mapping for requestDescriptor
Class class = [MyRequest class];
RKObjectMapping* reqMapping = [RKObjectMapping mappingForClass:class];
[reqMapping addAttributeMappingsFromDictionary:@{@"Name":@"name"}];
[reqMapping addPropertyMapping:relatedMapping];
RKRequestDescriptor *reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[reqMapping inverseMapping] objectClass:class rootKeyPath:nil method:RKRequestMethodPOST];

有个问题,当我通过RKObjectManager发帖时,服务器收到我的帖子总是

    {"Name":"my name", "Location":[]}

位置数组值似乎没有正确映射,有什么问题吗?

【问题讨论】:

  • 添加跟踪日志,是什么意思

标签: ios objective-c restkit


【解决方案1】:

最后我发现根本原因只是请求的 mime 类型设置。当我使用时:

[[RKObjectManager sharedManager] setRequestSerializationMIMEType:RKMIMETypeJSON];

一切顺利! ;)

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多