使用 NSPredicates/KVC 获取兄弟键数组
另一种表达方式可能是......
NSPredicate "state.country == 'United States'"
就像
SQL "Select * from state where state.country = 'United States'
我该如何将其作为谓词来做到这一点?
SQL "Select state.name from state where state.county = 'United States'"
原始帖子:
我有一本字典数组的字典,如下所示:
lists
states
state
country
country
country
我有按国家/地区过滤州的代码。不过,我敢打赌有一种更干净的方法。
NSArray *states = [lists valueForKey:@"states"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryname == %@", selectedCountry];
states = [states filteredArrayUsingPredicate:predicate];
states = [states valueForKeyPath:@"state"];
有想法吗?
Another way to word this might be...
NSPredicate "state.country == 'United States'"
is like
SQL "Select * from state where state.country = 'United States'
so how do I do this as a predicate?
SQL "Select state.name from state where state.county = 'United States'"
ORIGINAL POST:
I have a dictionary of arrays of dictionaries that looks like this:
lists
states
state
country
country
country
I have code to filter states by country. However, I'm betting there is a cleaner way.
NSArray *states = [lists valueForKey:@"states"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryname == %@", selectedCountry];
states = [states filteredArrayUsingPredicate:predicate];
states = [states valueForKeyPath:@"state"];
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎已经明白了:
这是你的原始字典。
这是从你的字典中检索状态数组。据推测,这些是实际的“State”对象(即,您创建了一个名为
State
的类)。这将创建一个谓词,将
-[State Countryname]
的结果与值进行比较由selectedCountry
变量引用。或者,如果states
数组包含字典,则会将-[NSDictionary objectForKey:@"countryname"]
的结果与selectedCountry
引用的值进行比较代码>.这将从数组中检索所有州,其中
[[aState Countryname] isEqual:selectedCountry]
这将创建一个包含每个州名称的新数组。这个新数组的顺序将与过滤后的
states
数组的顺序相同。You've almost got it:
This is your original dictionaryl
This is to retrieve the array of states from your dictionary. Presumably these are actual "State" objects (ie, you made a class called
State
)This creates a predicate that will compare the results of
-[State countryname]
to the value referenced by theselectedCountry
variable. Alternatively, if thestates
array contains dictionaries, this will compare the results of-[NSDictionary objectForKey:@"countryname"]
to the value referenced byselectedCountry
.This will retrieve all the states from the array where
[[aState countryname] isEqual:selectedCountry]
This will create a new array that contains the name of each state. This new array will be in the same order as your filtered
states
array.我只使用 NSArray 而不是 NSDictionary,所以我不知道这有多么有用,但我正在使用它:
其中 self.tours 是 Tour 对象的 NSArray。 Tour 对象具有 NSString *tourCode 属性。 matchingTour 是 NSArray 类型。
当我读到这篇文章并尝试它并且它第一次工作时我很惊讶,我得到了一个只包含一次旅行的数组!
在你的“树”中,你没有显示任何属性名称“国家/地区名称” - 只是想知道。
I'm only using NSArray and not NSDictionary so I don't know how useful this is but I'm using this:
where self.tours is an NSArray of Tour objects. A Tour object has a property of NSString *tourCode. matchingTour is of type NSArray.
I was amazed when I read about this, tried it and it worked first time and I got back an array containing just the one tour!
In your 'tree' you don't show any property name 'countryname' - just wondering.