发送 Array with unrest 和 objective c
Send Array with unrest and objective c
我正在 IOS 中的一个小项目中工作,该项目使用 unirest 库发送 json 消息。该请求包含一个数组,它给出了错误,我不知道为什么。
这是代码:
NSMutableDictionary * parameters = [[NSMutableDictionary alloc] init];
NSString *valueMd5 = @"examplemd5file";
[parameters setValue:valueMd5 forKey:@"md5file"];
NSString *path = @"pathfile";
[parameters setValue:path forKey:@"path"];
float latitude = 1.0f;
NSString *strLatitude = [NSString stringWithFormat:@"%f", latitude];
[parameters setValue:strLatitude forKey:@"latitude"];
float longitude = 1.0f;
NSString *strLongitude = [NSString stringWithFormat:@"%f", longitude];
[parameters setValue:strLongitude forKey:@"longitude"];
int image_width = 100;
NSString *strImageWidth = [NSString stringWithFormat:@"%d", image_width];
[parameters setValue:strImageWidth forKey:@"image_width"];
int image_height = 100;
NSString *strImageHeight = [NSString stringWithFormat:@"%d", image_height];
[parameters setValue:strImageHeight forKey:@"image_height"];
//"dd-MM-yyyy HH:mm:ss"
NSString *data_taken = @"11-12-1111 12:12:12";
[parameters setValue:data_taken forKey:@"data_taken"];
NSArray *tags = @[@"tag1", @"tag2"];
// tags = @[NSArray arrayWithObjects:@"tag1", @"tag2"];
[parameters setValue:tags forKey:@"tags"];
NSString* iaError = @"Error description";
[parameters setValue:iaError forKey:@"iaError"];
NSString *token = @"5aecf374c3dc09.43880964";
NSString* complete_url = [NSString stringWithFormat:@"%@/api/batch/metadata/%@", URL_BASE, token];
[[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:complete_url];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
// This is the asyncronous callback block
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
}];
这是错误:
2018-05-08 18:09:28.063517+0200 tflite_simple_example[6600:187624]
-[__NSArrayI length]: unrecognized selector sent to instance 0x600000238220 2018-05-08 18:09:28.073351+0200
tflite_simple_example[6600:187624] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]:
unrecognized selector sent to instance 0x600000238220'
根据 NSURLRequest
我猜 UNIRest
在后台使用(或使用类似的方法),HTTPHeaders 字段是 NSDictionary<NSString *,NSString *>
: NSDictionary
其中键是 NSString
并且值为 NSString
.
见NSURLRequest
或NSMutableURLRequest
对应的methods/properties。
@property(readonly, copy) NSDictionary<NSString *,NSString *> *allHTTPHeaderFields;
- (NSString *)valueForHTTPHeaderField:(NSString *)field;
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
- (void)addValue:(NSString *)value
forHTTPHeaderField:(NSString *)field;
所以你不能把一个NSArray
对象作为一个值。 iOS 需要一个 NSString,这说明您收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]:
unrecognized selector sent to instance 0x600000238220'
在那里,我们认识到调用的方法是 length
(NSString
Objective-C 中的基本方法)试图在 NSArray
对象而不是 NSString
一个。由于 NSArray
没有实现它,因此导致了错误和崩溃。
可能很难说出原因是因为它是调用length
的Cocoa的内部隐藏代码(你自己看不到)。但是知道在哪里,并假设 length
是针对 NSString
对象,并且罪魁祸首也是 NSArray
对象可以帮助您查明问题:
NSArray *tags = @[@"tag1", @"tag2"];
[parameters setValue:tags forKey:@"tags"];
所以 tags
需要是一个 NSString
对象而不是 NSArray
对象。
根据您的说法,需要使用 JSON 字符串化来调用 API。
所以:
NSArray *tags = @[@"tag1", @"tag2"];
NSData *tagsJSONData = [NSJSONSerialization dataWithJSONObject:tags options:0 error:nil]; //Might be useful to use a `NSError` object just in case
NSString *tagsJSONString = [[NSString alloc] initWithData:tagsJSONData encoding:NSUTF8StringEncoding];
[parameters setValue:tags forKey:@"tags"];
正如我在评论中所假设的那样,如果您的 API 需要用逗号分隔的标签:
NSArray *tags = @[@"tag1", @"tag2"];
NSString *tagsString = [tags componentsJoinedByString:@","];
[parameters setValue:tags forKey:@"tags"];
我正在 IOS 中的一个小项目中工作,该项目使用 unirest 库发送 json 消息。该请求包含一个数组,它给出了错误,我不知道为什么。
这是代码:
NSMutableDictionary * parameters = [[NSMutableDictionary alloc] init];
NSString *valueMd5 = @"examplemd5file";
[parameters setValue:valueMd5 forKey:@"md5file"];
NSString *path = @"pathfile";
[parameters setValue:path forKey:@"path"];
float latitude = 1.0f;
NSString *strLatitude = [NSString stringWithFormat:@"%f", latitude];
[parameters setValue:strLatitude forKey:@"latitude"];
float longitude = 1.0f;
NSString *strLongitude = [NSString stringWithFormat:@"%f", longitude];
[parameters setValue:strLongitude forKey:@"longitude"];
int image_width = 100;
NSString *strImageWidth = [NSString stringWithFormat:@"%d", image_width];
[parameters setValue:strImageWidth forKey:@"image_width"];
int image_height = 100;
NSString *strImageHeight = [NSString stringWithFormat:@"%d", image_height];
[parameters setValue:strImageHeight forKey:@"image_height"];
//"dd-MM-yyyy HH:mm:ss"
NSString *data_taken = @"11-12-1111 12:12:12";
[parameters setValue:data_taken forKey:@"data_taken"];
NSArray *tags = @[@"tag1", @"tag2"];
// tags = @[NSArray arrayWithObjects:@"tag1", @"tag2"];
[parameters setValue:tags forKey:@"tags"];
NSString* iaError = @"Error description";
[parameters setValue:iaError forKey:@"iaError"];
NSString *token = @"5aecf374c3dc09.43880964";
NSString* complete_url = [NSString stringWithFormat:@"%@/api/batch/metadata/%@", URL_BASE, token];
[[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:complete_url];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
// This is the asyncronous callback block
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
}];
这是错误:
2018-05-08 18:09:28.063517+0200 tflite_simple_example[6600:187624] -[__NSArrayI length]: unrecognized selector sent to instance 0x600000238220 2018-05-08 18:09:28.073351+0200 tflite_simple_example[6600:187624] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x600000238220'
根据 NSURLRequest
我猜 UNIRest
在后台使用(或使用类似的方法),HTTPHeaders 字段是 NSDictionary<NSString *,NSString *>
: NSDictionary
其中键是 NSString
并且值为 NSString
.
见NSURLRequest
或NSMutableURLRequest
对应的methods/properties。
@property(readonly, copy) NSDictionary<NSString *,NSString *> *allHTTPHeaderFields;
- (NSString *)valueForHTTPHeaderField:(NSString *)field;
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
- (void)addValue:(NSString *)value
forHTTPHeaderField:(NSString *)field;
所以你不能把一个NSArray
对象作为一个值。 iOS 需要一个 NSString,这说明您收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x600000238220'
在那里,我们认识到调用的方法是 length
(NSString
Objective-C 中的基本方法)试图在 NSArray
对象而不是 NSString
一个。由于 NSArray
没有实现它,因此导致了错误和崩溃。
可能很难说出原因是因为它是调用length
的Cocoa的内部隐藏代码(你自己看不到)。但是知道在哪里,并假设 length
是针对 NSString
对象,并且罪魁祸首也是 NSArray
对象可以帮助您查明问题:
NSArray *tags = @[@"tag1", @"tag2"];
[parameters setValue:tags forKey:@"tags"];
所以 tags
需要是一个 NSString
对象而不是 NSArray
对象。
根据您的说法,需要使用 JSON 字符串化来调用 API。
所以:
NSArray *tags = @[@"tag1", @"tag2"];
NSData *tagsJSONData = [NSJSONSerialization dataWithJSONObject:tags options:0 error:nil]; //Might be useful to use a `NSError` object just in case
NSString *tagsJSONString = [[NSString alloc] initWithData:tagsJSONData encoding:NSUTF8StringEncoding];
[parameters setValue:tags forKey:@"tags"];
正如我在评论中所假设的那样,如果您的 API 需要用逗号分隔的标签:
NSArray *tags = @[@"tag1", @"tag2"];
NSString *tagsString = [tags componentsJoinedByString:@","];
[parameters setValue:tags forKey:@"tags"];