从 plist NSString 添加多个单词的字典
我一直在使用以下内容从字典 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在手动检查部分类型。如果您想继续这种方式,您真正需要做的就是检查并查看该部分字符串是否包含您正在评估的特定部分的子字符串:
如何检查 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.
请注意,向 nil 发送任何消息都是合法的,因此如果
section
最终不是coughing
或headache
也没关系。另一方面,您还可以在发现新键时向ailmentsBySection
动态添加新键,而不仅仅是允许咳嗽
和头痛
。Note that it's legal to send any message to nil, so it's ok if
section
ends up being something other thancoughing
orheadache
. On the other hand you could also dynamically add new keys toailmentsBySection
as you find them instead of just allowingcoughing
andheadache
.