AFNetworking中通过参数的对象数组

时间:2017-06-13 16:48:43

标签: ios objective-c afnetworking afnetworking-3

我能够POST parameters如下所示,如果每个字典项只有一个项目,它就可以工作。在以下参数中,我只有一个pName和一个price

NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
                             @{@"pName":pData.pName, 
                             @"price":pData.price,
                             @"notes":pData.notes}];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:URL_SIGNIN parameters:params progress: nil
      success:^(NSURLSessionTask *operation, id responseObject)
 {
     NSLog(@"JSON: %@", responseObject);
 }
 failure:
 ^(NSURLSessionTask *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

但是,我想知道如果我有多个项目,例如pNames = [Beef, Coffee, Rice ,Sprite]prices = ["$10", $"3", "$5", @"1"],我怎么能把项目作为参数。

最后考虑以下对象。

orders = {@"Beef" : @"$10",@"Coffee" : @"$3", @"Rice" : @"$5", @"Sprite" : @"$1"}

假设是用户选择多个项目进行检查的餐馆应用程序。

1 个答案:

答案 0 :(得分:1)

你想要一个数组($[]),而不是字典(${})。

在你的例子中:

prices = @[@"$10", @"$3", @"$5", @"$1"];

编辑:

作为参数:

NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
                             @{@"prices": @[@"$10", @"$3", @"$5", @"$1"]}];

OR:

NSArray *prices = @[@"$10", @"$3", @"$5", @"$1"];
NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
                                 @{@"prices": prices];