2017-06-13 21 views
0

我能够POSTparameters如下,它适用于如果我只有一个项目的每个字典项目。在以下参数中,我只有一个pName和一个price通过AFNetworking中的参数对象的数组

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

根据Web API的文档:'的NSArray * params = @ [@ {@“pName”:pData.pName,@“price”: pData.price,@“notes”:pData.notes},@ {@“pName”:pData2.pName,@“price”:pData2.price,@“notes”:pData2.notes}等];'或'@pData2.price',@ [pData.price,pData2.price等],@“notes”:@ [pData.price,pData2.price等] pData.notes,pData2.notes,...]},但第一个解决方案似乎更多的逻辑。 – Larme

+0

非常感谢Larme,感谢您介绍面向对象的方式。 – hotspring

回答

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]; 
+0

我该如何将它们作为参数? – hotspring

+0

@hotspring请参阅编辑 –

+0

有没有更好的方法来解决作为对象的问题:Order = {@“Beef”:@“$ 10”,@“Coffee”:@“$ 3”,@“Rice”:@ $ 5“,@”Sprite“:@”$ 1“}'或者我应该用价格和名称来区分它们吗? – hotspring