简体   繁体   English

如何在NSDictionary中存储NSArray?

[英]How to store an NSArray in an NSDictionary?

I'm trying to teach myself but am struggling with how to store something like an NSArray inside an NSDictionary. 我正在努力教自己,但我正在努力如何在NSDictionary中存储像NSArray这样的东西。

Let's say hypothetically you had a NSDictionary for recipes: 假设您假设有一个NSDictionary用于食谱:

Let's say the NSDictionary had keys like: 让我们说NSDictionary有如下键:

Spaghetti, Fettucine Alfredo, Grilled Chicken Salad 意大利面,Fettucine Alfredo,烤鸡肉沙拉

And the NSDictionary had an NSArray which was simply a list of ingredients. NSDictionary有一个NSArray,它只是一个成分列表。

I suppose the equivalent PHP code would be something like: 我想相同的PHP代码是这样的:

$['Spaghetti'] = array('Spaghetti Pasta', 'Spaghetti Sauce', 'Meatballs');
$['Fettucine Alfredo'] = array('Fettucine Pasta', 'Alfredo Sauce');
$['Grilled Chicken Salad'] = array('Lettuce', 'Grilled Chicken', 'Croutons');

I'm struggling with how I can add an NSArray to the NSDictionary. 我正在努力如何将NSArray添加到NSDictionary中。 Then what if I wanted to remove an element or add an element to the array? 那么如果我想删除元素或向数组中添加元素呢? How is it retrieved and deleted or added to? 如何检索,删除或添加?

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
  [NSArray arrayWithObjects:@"Spaghetti Pasta", @"Tomato Sauce", nil], @"Spaghetti",
  [NSArray arrayWithObjects:@"Fettuccine Pasta", @"Alfredo Sauce", nil], @"Fettuccine Alfredo",
  [NSArray arrayWithObjects:@"Lettuce", @"Grilled Chicken", @"Croutons", nil], @"Grilled Chicken Salad",
  nil];

You can add any object type to a dictionary as a value. 您可以将任何对象类型作为值添加到字典中。 For more on dictionaries, see the documentation . 有关字典的更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM