Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用NSPredicates/KVC获取同级密钥数组_Iphone_Cocoa_Ios_Nspredicate_Key Value Coding - Fatal编程技术网

Iphone 使用NSPredicates/KVC获取同级密钥数组

Iphone 使用NSPredicates/KVC获取同级密钥数组,iphone,cocoa,ios,nspredicate,key-value-coding,Iphone,Cocoa,Ios,Nspredicate,Key Value Coding,另一种说法可能是 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

另一种说法可能是

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"];

想法?

我只使用NSArray,不使用NSDictionary,所以我不知道这有多有用,但我使用的是:

matchingTour = [self.tours filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tourCode == %@", tourCode]];
其中self.tours是一个旅游对象数组。巡更对象的属性为NSString*tourCode。matchingTour属于NSArray类型

当我读到这篇文章时,我很惊讶,尝试了一下,第一次就成功了,我得到了一个只包含一次巡演的阵列


在您的“树”中,您没有显示任何属性名称“countryname”-只是想知道。

我只使用NSArray而不是NSDictionary,所以我不知道这有多有用,但我使用的是:

matchingTour = [self.tours filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tourCode == %@", tourCode]];
其中self.tours是一个旅游对象数组。巡更对象的属性为NSString*tourCode。matchingTour属于NSArray类型

当我读到这篇文章时,我很惊讶,尝试了一下,第一次就成功了,我得到了一个只包含一次巡演的阵列

在您的“树”中,您没有显示任何属性名称“countryname”-只是想知道。

您几乎得到了它:

NSDictionary * lists = ...;
这是你原来的字典

NSArray *states = [lists objectForKey:@"states"];
这是为了从字典中检索状态数组。大概这些是实际的“状态”对象(即,您创建了一个名为
State
)的类)

这将创建一个谓词,将
-[State countryname]
的结果与
selectedCountry
变量引用的值进行比较。或者,如果
声明
数组包含字典,这将比较
-[NSDictionary objectForKey:@“countryname”]
的结果与
selectedCountry
引用的值

states = [states filteredArrayUsingPredicate:predicate];
这将从数组中检索所有状态,其中
[[aState countryname]isEqual:selectedCountry]

states = [states valueForKeyPath:@"name"];
这将创建一个包含每个状态名称的新数组。此新数组的顺序将与过滤后的
状态的顺序相同。

您几乎已经得到了它:

NSDictionary * lists = ...;
这是你原来的字典

NSArray *states = [lists objectForKey:@"states"];
这是为了从字典中检索状态数组。大概这些是实际的“状态”对象(即,您创建了一个名为
State
)的类)

这将创建一个谓词,将
-[State countryname]
的结果与
selectedCountry
变量引用的值进行比较。或者,如果
声明
数组包含字典,这将比较
-[NSDictionary objectForKey:@“countryname”]
的结果与
selectedCountry
引用的值

states = [states filteredArrayUsingPredicate:predicate];
这将从数组中检索所有状态,其中
[[aState countryname]isEqual:selectedCountry]

states = [states valueForKeyPath:@"name"];

这将创建一个包含每个状态名称的新数组。这个新数组的顺序将与过滤后的
状态
数组的顺序相同。

我可能遗漏了一些明显的内容,但你的结构图对我来说毫无意义。你能举个具体的例子吗?他想知道某个国家的国名。他找了库比蒂诺,得到了加利福尼亚,对吗?是的。州有名称和国家/地区字符串。获取拥有state.country==“美国”的州的列表。State类有State.State,State.country。我可能遗漏了一些明显的东西,但你的结构图对我来说毫无意义。你能举个具体的例子吗?他想知道某个国家的国名。他找了库比蒂诺,得到了加利福尼亚,对吗?是的。州有名称和国家/地区字符串。获取拥有state.country==“美国”的州的列表。State类有State.State,State.country。看起来这是最干净的方式。看起来这是最干净的方式。