如何将 NSArray 存储在 NSDictionary 中?

发布于 2024-09-08 10:22:26 字数 560 浏览 2 评论 0原文

我正在尝试自学,但正在努力解决如何在 NSDictionary 中存储 NSArray 等内容。

假设你有一个用于食谱的 NSDictionary:

假设 NSDictionary 有这样的键:

Spaghetti, 阿尔弗雷多宽面条, 烤鸡肉沙拉

NSDictionary 有一个 NSArray,它只是一个成分列表。

我想等效的 PHP 代码将类似于:

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

我正在努力解决如何将 NSArray 添加到 NSDictionary 的问题。那么如果我想删除一个元素或向数组添加一个元素怎么办?如何检索、删除或添加它?

I'm trying to teach myself but am struggling with how to store something like an NSArray inside an NSDictionary.

Let's say hypothetically you had a NSDictionary for recipes:

Let's say the NSDictionary had keys like:

Spaghetti,
Fettucine Alfredo,
Grilled Chicken Salad

And the NSDictionary had an NSArray which was simply a list of ingredients.

I suppose the equivalent PHP code would be something like:

$['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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-09-15 10:22:26
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];

您可以将任何对象类型作为值添加到字典中。有关词典的更多信息,请参阅 文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文