iosobjective-cafnetworkingafnetworking-3

Array of objects via parameters in AFNetworking


I could able to POST the parameters as follows, it works if I have only one item for each dictionary item. In the following params I have only one pName and one 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);
 }];

However, I wonder how could I able to put items as a parameters if I have more than one items such as pNames = [Beef, Coffee, Rice ,Sprite] and prices = ["$10", $"3", "$5", @"1"].

Consider as following object at the end.

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

Assume that it is restaurant application where user selects multiple items to check out.


Solution

  • You want an array ($[]), not a dictionary (${}).

    In your example:

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

    EDIT:

    As parameters:

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

    OR:

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