从 plist NSString 添加多个单词的字典

发布于 2024-12-14 00:33:34 字数 625 浏览 2 评论 0原文

我一直在使用以下内容从字典 plist 创建一个数组:

self.cough = [NSMutableArray arrayWithCapacity:[ailments count]];
NSDictionary* dict;
for (dict in ailments)
    if ([[dict valueForKey:@"section"]isEqualToString:@"coughing"])[cough addObject:dict];

plist 的格式是:

section:    coughing
name:       Common Cold

我遇到的麻烦,我怀疑它很简单,如果我想在其中包含“普通感冒”在不同的部分,例如“头痛”,我可以为新部分创建另一个疾病对象,但它会显示 2 个普通感冒条目(来自“咳嗽”和“头痛”),从而弄乱了我的搜索结果。

我想做的是:

section:    coughing, headache
name:       Common Cold

我会使用什么来代替 isEqualToString: 来创建两个不同的数组,一个用于“咳嗽”,另一个用于“头痛”?

I've been using the following to create an array from a plist of dictionaries:

self.cough = [NSMutableArray arrayWithCapacity:[ailments count]];
NSDictionary* dict;
for (dict in ailments)
    if ([[dict valueForKey:@"section"]isEqualToString:@"coughing"])[cough addObject:dict];

The format of the plist is:

section:    coughing
name:       Common Cold

The trouble I'm having, and I suspect its an easy one is, if I want to have "Common Cold" in a different section, like "Headache", I could create another ailment object for the new section but it messes up my search result by showing 2 Common Cold entries (from both "Coughing" and "Headache").

What I'd like to do is:

section:    coughing, headache
name:       Common Cold

What would I use instead of isEqualToString: to create two different arrays, one for "Coughing" and another for "Headache"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

过去的过去 2024-12-21 00:33:34

您似乎正在手动检查部分类型。如果您想继续这种方式,您真正需要做的就是检查并查看该部分字符串是否包含您正在评估的特定部分的子字符串:

如何检查 Objective-C 中的一个字符串是否包含另一个字符串?

但是,它可能不是静态定义您要查找的部分名称,而是有利于您考虑尝试获取部分从您的 plist 动态名称/类型,然后排序到这些部分。

It appears that you are manually checking against for section type. If you wanted to continue this way, all you really need to do is check and see if the section string contains the substring of the particular section you're evaluating for:

How do I check if a string contains another string in Objective-C?

However rather than statically define what section names you're looking for, it might benefit you to consider trying to obtain sections names/types dynamically from your plist and then sorting into those sections.

兮子 2024-12-21 00:33:34
NSMutableDictionary *ailmentsBySection = [NSMutableDictionary dictionary];
[ailmentsBySection addObject:[NSMutableArray array] forKey:@"coughing"];
[ailmentsBySection addObject:[NSMutableArray array] forKey:@"headache"];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
for (NSDictionary *ailment in ailments) {
    NSString *sections = [ailment objectForKey:@"section"];
    for (NSString *section in [sections componentsSeparatedByString:@","]) {
        section = [section stringByTrimmingCharactersInSet:whitespace];
        [[ailmentsBySection objectForKey:section] addObject:ailment];
    }
}

请注意,向 nil 发送任何消息都是合法的,因此如果 section 最终不是 coughingheadache 也没关系。另一方面,您还可以在发现新键时向 ailmentsBySection 动态添加新键,而不仅仅是允许咳嗽头痛

NSMutableDictionary *ailmentsBySection = [NSMutableDictionary dictionary];
[ailmentsBySection addObject:[NSMutableArray array] forKey:@"coughing"];
[ailmentsBySection addObject:[NSMutableArray array] forKey:@"headache"];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
for (NSDictionary *ailment in ailments) {
    NSString *sections = [ailment objectForKey:@"section"];
    for (NSString *section in [sections componentsSeparatedByString:@","]) {
        section = [section stringByTrimmingCharactersInSet:whitespace];
        [[ailmentsBySection objectForKey:section] addObject:ailment];
    }
}

Note that it's legal to send any message to nil, so it's ok if section ends up being something other than coughing or headache. On the other hand you could also dynamically add new keys to ailmentsBySection as you find them instead of just allowing coughing and headache.

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