Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用RestKit发布字符串的JSON数组_Objective C_Json_Mapping_Restkit - Fatal编程技术网

Objective c 使用RestKit发布字符串的JSON数组

Objective c 使用RestKit发布字符串的JSON数组,objective-c,json,mapping,restkit,Objective C,Json,Mapping,Restkit,我有以下目标 @interface MyObject : NSObject @property(nonatomic, copy) NSNumber *objectId; @property(nonatomic, copy) NSArray *tags; // array of strings, e.g. @[@"one", @"two"] @end 我想使用RestKit(版本0.23)将请求发布到URL 我已经确定了路线 RKRoute *route = [RKRoute routeWi

我有以下目标

@interface MyObject : NSObject

@property(nonatomic, copy) NSNumber *objectId;
@property(nonatomic, copy) NSArray *tags; // array of strings, e.g. @[@"one", @"two"]

@end
我想使用RestKit(版本0.23)将请求发布到URL

我已经确定了路线

RKRoute *route = [RKRoute routeWithClass: [MyObject class] pathPattern: do_something/:objectId method: RKRequestMethodPOST];
[objectManager.router.routeSet addRoute: route];
现在,我想创建一个请求

[objectManager requestWithObject: instanceOfMyObject method: RKRequestMethodPOST path: nil parameters: nil];
我应该如何配置
[RKObjectMapping requestMapping]
以及如何定义
RKRequestDescriptor
来获取上面的映射(字符串的JSON数组)

我尝试了以下方法:

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addPropertyMapping: [RKAttributeMapping attributeMappingFromKeyPath: @"tags" toKeyPath: nil]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping: requestMapping objectClass: [MyObject class] rootKeyPath: nil method: RKRequestMethodAny];
但是,使用
nil
作为
RKAttributeMapping
中的关键路径会崩溃。使用任何NOTNIL值都会导致请求的主体是JSON字典

{"someKeyPath": ["one", "two"]}

我相信RestKit的设计理念是你应该发布一个对象,例如
{“tags”:[“tag1”,“tag2”]}
。回答也一样;如果主体不是对象(只是一个原始字符串或数组),RestKit将无法很好地处理它

也就是说,您可以自己序列化请求并将其发布到正文中。例如:

NSMutableURLRequest *request = [[objectManager requestWithObject:nil method:RKRequestMethodPOST path:@"do_something/objectId" parameters:nil] mutableCopy];

[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:doableIds options:0 error:nil]];

RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request success:nil failure:nil];
[objectManager enqueueObjectRequestOperation:operation];

您是否尝试过创建映射和描述符?显示您尝试过的代码,并解释错误所在。@Wain Question edited。在这里只使用NSJSONSerialization更容易case@Wain请问“更容易”是什么意思?这是否意味着不可能配置RKRequestDescriptor来创建这样的请求?您实际上并没有进行任何映射。我没有尝试过像您这样获得一个简单的数组,所以我不知道异常是什么,但是使用NSJSONSerialization创建主体数据,然后使用RK创建请求,添加主体并使用RK发送它是一个很好的解决方案
{"someKeyPath": ["one", "two"]}
NSMutableURLRequest *request = [[objectManager requestWithObject:nil method:RKRequestMethodPOST path:@"do_something/objectId" parameters:nil] mutableCopy];

[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:doableIds options:0 error:nil]];

RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request success:nil failure:nil];
[objectManager enqueueObjectRequestOperation:operation];