[英]Extracting a key/value pair from an NSDictionary
Is there a convenient way to obtain both a key/value pair from an NSDictionary? 是否有从NSDictionary获取键/值对的便捷方法?
Say I have a NSDictionary, partyGuest, 假设我有一个NSDictionary,partyGuest,
{
name = "jim";
age = 28;
occupation = "astronaut";
favouriteMeal =
{
starter = "fish head soup";
mainCourse = "roast armadillo";
dessert = "sugar plum fairy cakes";
}
}
I'd like to get obtain a key/value pair within that, like so, 我想在其中获取键/值对,就像这样,
NSDictionary *guestFoodChoice = [partyGuest itemForKey:@"favouriteMeal"];
...and have that obtain both the key and the value, ...并获得键和值,
guestFoodChoice =
{
favouriteMeal =
{
starter = "fish head soup";
mainCourse = "roast armadillo";
dessert = "sugar plum fairy cakes";
}
}
It seems there should be, but as I can't see an obvious method, maybe I'm missing something? 似乎应该有,但是由于我看不到明显的方法,也许我缺少了什么?
It sounds like you basically want to get a dictionary with some subset (maybe just one) of key-value pairs in another dictionary. 听起来您基本上是想获得一本字典,其中包含另一个字典中的键/值对的某些子集(也许只是一个)。 If that's right, the Key-Value Coding method
dictionaryWithValuesForKeys:
is what you want. 如果是正确的话,键-值编码方法
dictionaryWithValuesForKeys:
就是您想要的。
NSDictionary *guestFoodChoice = [partyGuest dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"favouriteMeal"]];
NSDictionary *guestFoodChoice = [NSDictionary dictionaryWithObjectsAndKeys:[partyGuest itemForKey:@"favouriteMeal"],@"favouriteMeal",nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.