Search code examples
iosobjective-cxcodeafnetworking

AFNetworking - NSMutableDictionary parameters


I'm trying to build my NSMutableDictionary to send to a request using the AFNetworking framework, but it seems that I'm quite confused about how to do it properly.

Here's what the server is expecting

{
"do":"timeline",
"what":"posting",
"session":"",
"data":{
"status_timeline":"",
    "with_timeline":"",
    "location_timeline":"",
    "category_timeline":"",
    "privacy_timeline":""  
}

and I've tried like this

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:@"timeline" forKey:@"do"];
    [dict setValue:@"posting" forKey:@"what"];
    [dict setValue:session forKey:@"session"];
    NSLog(@"Session %@",  [dict  valueForKey:@"session"]);

I hope someone can help me thank's


Solution

  • You can use below to get input what your server want. Make sure your server will decode the data from dictionary.

      NSMutableDictionary *dicData = [NSMutableDictionary new];
        dicData[@"status_timeline"] = @"";
        dicData[@"with_timeline"] = @"";
        dicData[@"location_timeline"] = @"";
        dicData[@"category_timeline"] = @"";
        dicData[@"privacy_timeline"] = @"";
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicData options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setValue:@"timeline" forKey:@"do"];
        [dict setValue:@"posting" forKey:@"what"];
        [dict setValue:@"" forKey:@"session"];
        [dict setValue:jsonString forKey:@"data"];
        NSLog(@"Your Main Dic: %@",  dict);