另一种表达方式可能是......
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"];
想法?
最佳答案
你几乎已经明白了:
NSDictionary * lists = ...;
这是你原来的字典
NSArray *states = [lists objectForKey:@"states"];
这是从字典中检索状态数组。据推测,这些是实际的“状态”对象(即,您创建了一个名为 State
的类)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryname == %@", selectedCountry];
这将创建一个谓词来比较 -[State countryname]
的结果到 selectedCountry
引用的值多变的。或者,如果states
数组包含字典,这将比较 -[NSDictionary objectForKey:@"countryname"]
的结果到 selectedCountry
引用的值.
states = [states filteredArrayUsingPredicate:predicate];
这将从数组中检索 [[aState countryname] isEqual:selectedCountry]
的所有状态。
states = [states valueForKeyPath:@"name"];
这将创建一个包含每个州名称的新数组。这个新数组的顺序将与您过滤后的 states
的顺序相同。数组。
关于iphone - 使用 NSPredicates/KVC 获取兄弟键数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4512612/