NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
arrayOfRestaurants = searchResponseJSON[@"businesses"];
NSDictionary *businesses = [arrayOfRestaurants firstObject];
NSString *businessesID = businesses[@"id"];
我试图将数组中的所有数据都放入nsdictionary而不是第一个对象。有一个更好的方法吗?我似乎无法从数组中获取所有数据到nsdictionary
旁注:我对Objective c
非常新答案 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)
NSDictionary
是unordered 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];
将对象添加到数组