代码之家  ›  专栏  ›  技术社区  ›  carlos.baez

发送带有不安和目标c的数组

  •  0
  • carlos.baez  · 技术社区  · 6 年前

    我在IOS中的一个小项目中工作,该项目使用 unirest lib。请求包含一个数组,它给出了错误,我不知道为什么。

    代码如下:

    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\u simple\u示例[6600:187624] -[\u NSArrayI length]:发送到实例0x600000238220 2018-05-08 18:09:28.073351+0200的无法识别的选择器 tflite\u simple\u示例[6600:187624]***由于未捕获而终止应用程序 异常“NSInvalidArgumentException”,原因:“-[\uu NSArrayI length]: 发送到实例0x600000238220的选择器无法识别'

    1 回复  |  直到 6 年前
        1
  •  0
  •   Larme    6 年前

    根据 NSURLRequest 我想是吧 UNIRest 在后台使用(或使用类似的方法),HTTPHeaders字段是 NSDictionary<NSString *,NSString *> : NSDictionary 钥匙所在位置 NSString 以及值的位置 NSString字符串

    请参阅的相应方法/属性 NSURLRequest NSMutableURLRequest

    @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,这说明您会遇到此错误:

    ***由于未捕获的异常“NSInvalidArgumentException”,正在终止应用程序,原因:“-[\uu NSArrayI length]: 发送到实例0x600000238220的选择器无法识别'

    在这里,我们认识到调用的方法是 length ( NSString字符串 目标C)中的基本目标 不可变数组 对象而不是 NSString字符串 一自从 不可变数组 没有实现它,这导致了错误和崩溃。 可能很难说出原因是因为可可的内部隐藏代码 (你自己看不到)。但是知道在哪里,并且假设 是为 NSString字符串 对象,而且罪犯也是 不可变数组 对象帮助您查明问题:

    NSArray *tags  = @[@"tag1", @"tag2"];
    [parameters setValue:tags forKey:@"tags"];
    

    所以 tags 需要成为 NSString字符串 对象而不是 不可变数组 一 根据您的说法,需要使用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"];