使用 NSPredicates/KVC 获取兄弟键数组

发布于 2024-10-09 11:15:42 字数 774 浏览 0 评论 0原文

另一种表达方式可能是......

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-10-16 11:15:42

你几乎已经明白了:

NSDictionary * lists = ...;

这是你的原始字典。

NSArray *states = [lists objectForKey:@"states"];

这是从你的字典中检索状态数组。据推测,这些是实际的“State”对象(即,您创建了一个名为 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 数组的顺序相同。

You've almost got it:

NSDictionary * lists = ...;

This is your original dictionaryl

NSArray *states = [lists objectForKey:@"states"];

This is to retrieve the array of states from your dictionary. Presumably these are actual "State" objects (ie, you made a class called State)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryname == %@", selectedCountry];

This creates a predicate that will compare the results of -[State countryname] to the value referenced by the selectedCountry variable. Alternatively, if the states array contains dictionaries, this will compare the results of -[NSDictionary objectForKey:@"countryname"] to the value referenced by selectedCountry.

states = [states filteredArrayUsingPredicate:predicate];

This will retrieve all the states from the array where [[aState countryname] isEqual:selectedCountry]

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

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.

面如桃花 2024-10-16 11:15:42

我只使用 NSArray 而不是 NSDictionary,所以我不知道这有多么有用,但我正在使用它:

matchingTour = [self.tours filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tourCode == %@", tourCode]];

其中 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:

matchingTour = [self.tours filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tourCode == %@", tourCode]];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文