ios - NSMutableString的appendFormat不能追加多个项

标签 ios objective-c nsmutablestring

我使用了NSMutableString并使用了appendFormat函数,appendFormat设置了边界和第一个图像,并且没有追加任何其他项目,我不知道错误在哪里。
导致调试:-

2016-03-08 15:52:56.385 PropertyTurkey [474:82387]
正文----- = ----- 011000010111000001101001

Content-Disposition:表单数据; name =“image []”;
filename =“/ var / mobile / Containers / Data / Application / A3EA5D3E-377E-4E16-9541-580BB57E211E / Documents / 1.jpg”

ÿØÿà

NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",
                                   @"cache-control": @"no-cache" };
        NSArray *parameters = @[ @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[0]]},
                                 @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[1]]},
                                 @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[2]]},
                                 @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[3]]},
                                 @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[4]]},
                                 @{ @"name": @"rea_title", @"value": [NSString stringWithFormat:@"%@",_realEstateTitle] },
                                 @{ @"name": @"why_buy_property", @"value": @"why_buy_property" },
                                 @{ @"name": @"cit_id", @"value": [NSString stringWithFormat:@"%@",_cit_id]  },
                                 @{ @"name": @"are_id", @"value": [NSString stringWithFormat:@"%@",_are_id] },
                                 @{ @"name": @"garden_exterior", @"value":[NSString stringWithFormat:@"%@",_desOfGardenAndExter] },
                                 @{ @"name": @"home_interior", @"value":[NSString stringWithFormat:@"%@",_desOfHomeInterior] },
                                 @{ @"name": @"property_desc", @"value":[NSString stringWithFormat:@"The property desc field is required."] },
                                 @{ @"name": @"typ_id", @"value": [NSString stringWithFormat:@"%@",_typ_id] },
                                 @{ @"name": @"rea_bedrooms", @"value": [NSString stringWithFormat:@"%@",_bedrooms] },
                                 @{ @"name": @"rea_bathrooms", @"value": [NSString stringWithFormat:@"%@",_bathrooms]},
                                 @{ @"name": @"rea_living_space", @"value": [NSString stringWithFormat:@"%@",_livingSpaceSqm] },
                                 @{ @"name": @"sta_id", @"value": [NSString stringWithFormat:@"%@",_sta_id]},
                                 @{ @"name": @"rea_price", @"value": [NSString stringWithFormat:@"%@",_price]} ];

        NSString *boundary = @"---011000010111000001101001";

        NSError *error;
        NSMutableString *body = [[NSMutableString alloc]init];
        for (NSDictionary *param in parameters) {
        [body appendFormat:@"--%@\r\n", boundary];
            if (param[@"fileName"]) {
                [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
                //[body appendFormat:@"Content-Type: %@\r\n\r\n", headers[@"content-type"]];
                NSLog(@"%@",headers[@"content-type"]);
                [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSISOLatin1StringEncoding error:&error]];
                if (error) {
                    NSLog(@"%@", error);
                }
            } else {
                [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
                [body appendFormat:@"%@", param[@"value"]];
            }
            NSLog(@"Body-----=%@",body);
        }
        [body stringByAppendingFormat:@"\r\n--%@--\r\n", boundary];
        NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.123/api/v1/sellProperty?api_key=%@&auth_token=%@&device_token=%@",apiKey,check,deviceToken]]
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:10.0];
        [request setHTTPMethod:@"POST"];
        [request setAllHTTPHeaderFields:headers];
         NSLog(@"%@",headers);

        [request setHTTPBody:postData];

        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                        if (error) {
                                                            NSLog(@"%@", error);
                                                        } else {
                                                             NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
                                                            NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                                                            NSLog(@"%@", httpResponse);
                                                             NSLog(@"res %@ serializedData %@", response,serializedData);

                                                        }
                                                    }];
        [dataTask resume];

最佳答案

您正在使用stringByAppendingFormat来创建一个新字符串,而不向现有字符串添加任何内容。

关于ios - NSMutableString的appendFormat不能追加多个项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869339/

相关文章:

objective-c - NSDateFormatter dateFromString与日期

ios - 核心数据 - 如何跟踪实体集的子集

objective-c - NSString 在源 NSMutableString 的 "blanking"之后不保留值

ios - 检查多个 UISwitch 的状态

iOS 通用触摸处理程序

objective-c - objective-c : convert a NSMutableString in NSString

xcode - 将多个字符串添加到一个字符串

ios - 如何将我们在健康应用程序中输入的高度和体重数据读取到我们的自定义应用程序

iphone - UIImageView 转换缩放

iphone - nslog的iPhone控制台?