计算 Info.plist 中多个 NSDictionnary 中特定值的出现次数

发布于 2024-10-19 08:44:51 字数 1491 浏览 5 评论 0原文

我有一个 .plist 文件,在应用程序中使用它来存储数据,并检索字典中的所有内容。

plist 如下所示:

(
    {
    firstName = "18:43";
    imageFilePath = "exclamation.png";//     <--- THIS is what I want to track.
    lastName = "25/02/2010";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
},
    {
    firstName = "16:43";
    imageFilePath = "accept.png";//          <--- NOT this
    lastName = "01/01/2011";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
},
    {
    firstName = "16:43";
    imageFilePath = "exclamation.png";//     <--- THIS
    lastName = "25/02/2011";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
}
)

等等...

我只想拥有一个 int (或 NSNumber,或NSInteger,我不太关心)这是我的 plist 中出现了多少次“exclamation.png”的值。 (在上面的例子中,这将是“2”) (这个数字将成为应用程序的徽章编号)

我尝试了很多不同的事情(包括将 plist 转换为字符串并搜索,但这很糟糕)但我无法使它们中的任何一个工作......

谢谢你的想法!

编辑:

这就是我加载 plist 的方式:

- (void)applicationDidEnterBackground:(UIApplication *)application {
[_listController save];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Books.plist"];

// The code here....    

}

I have a .plist file that I use in my application for storing data, and I retrieve all the stuff in a dictionary.

Here's what the plist looks like :

(
    {
    firstName = "18:43";
    imageFilePath = "exclamation.png";//     <--- THIS is what I want to track.
    lastName = "25/02/2010";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
},
    {
    firstName = "16:43";
    imageFilePath = "accept.png";//          <--- NOT this
    lastName = "01/01/2011";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
},
    {
    firstName = "16:43";
    imageFilePath = "exclamation.png";//     <--- THIS
    lastName = "25/02/2011";
    lieu = "Class 045";
    prof = "Mr. Maths";
    publicationYear = 12;
    title = "Mathematics";
}
)

and so on...

What I just want to do is to have an int (or NSNumber, or NSInteger, I don't really care) that is the value of how many occurrences of "exclamation.png" there are in my plist. (in the example above, that would be '2')
(this number is then going to be the App's badgeNumber)

I tried a lot of different things (including converting the plist to a string and searching, but that went badly) but I couldn't make any of them work...

Thanks for your ideas !

EDIT :

This is how I load the plist :

- (void)applicationDidEnterBackground:(UIApplication *)application {
[_listController save];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Books.plist"];

// The code here....    

}

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

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

发布评论

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

评论(2

゛时过境迁 2024-10-26 08:44:51

[未经测试;可能编译]

NSArray *dicts = [NSArray arrayWithContentsOfFile:@"my.plist"];
NSUInteger count = 0;
for (NSDictionary *dict in dicts) {
    if ([[dict objectForKey:@"imageFilePath"] isEqualToString:@"exclamation.png"]) {
        ++count;
    }
}

[Untested; probably compiles]

NSArray *dicts = [NSArray arrayWithContentsOfFile:@"my.plist"];
NSUInteger count = 0;
for (NSDictionary *dict in dicts) {
    if ([[dict objectForKey:@"imageFilePath"] isEqualToString:@"exclamation.png"]) {
        ++count;
    }
}
月亮是我掰弯的 2024-10-26 08:44:51

[忽略:谓词仅在核心数据中更快 -- techzen]

这是一种更简洁、更快的方法:

NSArray *fromPlist=[NSArray arrayWithContentsOfFile:@"my.plist"];
NSPredicate *exPred=[NSPredicate predicateWithFormat:@"imageFilePath==%@",@"exclamation.png"];
// NSNumber *exclamationCount=[[fromPlist filteredArrayUsingPredicate:exPred].@count];
NSNumber *exclamationCount=[[[fromPlist filteredArrayUsingPredicate:exPred] valueForKey:@"@count"]];

最后一行表示:告诉 array fromPlist 返回另一个由与谓词测试匹配的对象组成的数组。然后获取返回数组的计数。

这比循环快得多(10>),特别是对于大型数组。更好的是,您的谓词可以任意复杂,即您可以在每个谓词中以多种不同的方式测试多个属性。您还可以保存谓词并重复使用它们,即使它们包含变量也是如此。

键值编码、集合运算符(例如@count)和谓词的组合非常强大。学会很好地使用它们可以在速度、可靠性和灵活性方面取得巨大的进步。

[Ignore: Predicates are only faster within Core Data -- techzen]

Here's a neater and faster way to do this:

NSArray *fromPlist=[NSArray arrayWithContentsOfFile:@"my.plist"];
NSPredicate *exPred=[NSPredicate predicateWithFormat:@"imageFilePath==%@",@"exclamation.png"];
// NSNumber *exclamationCount=[[fromPlist filteredArrayUsingPredicate:exPred].@count];
NSNumber *exclamationCount=[[[fromPlist filteredArrayUsingPredicate:exPred] valueForKey:@"@count"]];

The last line says: tell array fromPlist to return another array composed of objects that match the predicate test. Then take the count of the returned array.

This is much faster (10>) than a loop especially for large arrays. Even better, your predicate can be arbitrarily complex i.e. you can test multiple attributes in many different ways in each predicate. You can also save the predicates and reuse them even if they contain variables.

The combination of key-value coding, collection operators e.g. @count, and predicates is enormously powerful. Learning to use them well opens up huge improvements in speed, reliability and flexibility.

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