如何将 NSArray 存储在 NSDictionary 中?
我正在尝试自学,但正在努力解决如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将任何对象类型作为值添加到字典中。有关词典的更多信息,请参阅 文档。
You can add any object type to a dictionary as a value. For more on dictionaries, see the documentation.