将整个数组添加到nsdictionary

时间:2016-05-06 15:49:51

标签: ios objective-c nsarray

NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

arrayOfRestaurants = searchResponseJSON[@"businesses"];

NSDictionary *businesses = [arrayOfRestaurants firstObject];

NSString *businessesID = businesses[@"id"];

我试图将数组中的所有数据都放入nsdictionary而不是第一个对象。有一个更好的方法吗?我似乎无法从数组中获取所有数据到nsdictionary

旁注:我对Objective c

非常新

4 个答案:

答案 0 :(得分:0)

只需在数组上使用for循环迭代,你就可以从那里得到每个对象并做你的东西。在得到所有对象之后,我不需要你告诉我,我会编辑我的答案

 NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if [searchResponseJSON[@"businesses"] isKindofClass: [NSDictionary class]] {
    arrayOfRestaurants = dictionary;

    for NSDictionary * dictionary in arrayOfRestaurants {
    print("dictionary")
    }

}

答案 1 :(得分:0)

请尝试这个可能对您有所帮助

 NSArray * myArray = [NSArray arrayWithObjects:@"hello", @"hi", @"hey"];

 NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:@"threeLetters"];

 NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithCapacity:10];
 [mutableDict setObject:myArray forKey:@"threeLetters"];

答案 2 :(得分:0)

NSDictionaryunordered collection of objects,但每个包含的值都是associated with a key,,其作用类似于标签。

NSArray * array = [NSArray arrayWithObjects:@“One”,@“Two”@“Three”,@“Four”,nil];

1)直接插入数组元素,如下所示:

[NSDictionary initWithObjectsAndKeys:[NSArray arrayWithObjects:@"One",@"Two"@"Three",@"Four",nil], @"FirstKey", nil];

2)在NSDictionary中插入NSArray

NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:@"FirstKey"];

答案 3 :(得分:0)

  • NSDictionary * dictProducts = [response valueForKey:kViewModel];

  • 将对象添加到数组

  • [_ yourArray addObject:[dictProducts objectForKey:kProductID]];
  • NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
  • [dict setObject:_yourArray forKey:@“Your_Key”];
  • 您可以使用相同的密钥
  • 在此课程中使用此词典