将整个数组添加到 nsdictionary
Adding entire array to nsdictionary
NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
arrayOfRestaurants = searchResponseJSON[@"businesses"];
NSDictionary *businesses = [arrayOfRestaurants firstObject];
NSString *businessesID = businesses[@"id"];
我正在尝试将数组中的所有数据放入 nsdictionary 而不仅仅是第一个对象。有一个更好的方法吗?我似乎无法将数组中的所有数据放入 nsdictionary
旁注:我是 Objective c
的新手
只需在数组上使用 for 循环迭代,您将从那里获取每个对象并执行您的 stuff.i 在您获取所有对象后不需要什么,只需告诉我,我将编辑我的答案
NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if [searchResponseJSON[@"businesses"] isKindofClass: [NSDictionary class]] {
arrayOfRestaurants = dictionary;
for NSDictionary * dictionary in arrayOfRestaurants {
print("dictionary")
}
}
请试试这个,可能对你有帮助
NSArray * myArray = [NSArray arrayWithObjects:@"hello", @"hi", @"hey"];
NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:@"threeLetters"];
NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithCapacity:10];
[mutableDict setObject:myArray forKey:@"threeLetters"];
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"];
NSDictionary *dictProducts=[response valueForKey:kViewModel];
将对象添加到数组
[_yourArray addObject:[dictProducts objectForKey:kProductID]];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject: _yourArray forKey:@"Your_Key"];
您可以在class中使用相同的键
使用该词典
NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
arrayOfRestaurants = searchResponseJSON[@"businesses"];
NSDictionary *businesses = [arrayOfRestaurants firstObject];
NSString *businessesID = businesses[@"id"];
我正在尝试将数组中的所有数据放入 nsdictionary 而不仅仅是第一个对象。有一个更好的方法吗?我似乎无法将数组中的所有数据放入 nsdictionary
旁注:我是 Objective c
的新手只需在数组上使用 for 循环迭代,您将从那里获取每个对象并执行您的 stuff.i 在您获取所有对象后不需要什么,只需告诉我,我将编辑我的答案
NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if [searchResponseJSON[@"businesses"] isKindofClass: [NSDictionary class]] {
arrayOfRestaurants = dictionary;
for NSDictionary * dictionary in arrayOfRestaurants {
print("dictionary")
}
}
请试试这个,可能对你有帮助
NSArray * myArray = [NSArray arrayWithObjects:@"hello", @"hi", @"hey"];
NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:@"threeLetters"];
NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithCapacity:10];
[mutableDict setObject:myArray forKey:@"threeLetters"];
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"];
NSDictionary *dictProducts=[response valueForKey:kViewModel];
将对象添加到数组
[_yourArray addObject:[dictProducts objectForKey:kProductID]];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject: _yourArray forKey:@"Your_Key"];
您可以在class中使用相同的键
使用该词典