简体   繁体   English

从plist获取并比较CFBundleVersion

[英]Fetching and comparing CFBundleVersion from plist

I'm trying to compare CFBundleVersion key of 2 Apps inside com.apple.mobile.installation.plist which include the info of every installed application on iPhone 我正在尝试比较com.apple.mobile.installation.plist中2个应用程序的CFBundleVersion密钥,其中包括iPhone上每个已安装应用程序的信息

NSString *appBundleID =@"net.someapp.app";
NSString *appBundleID2=@"net.someapp.app2";

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:
         @"/var/mobile/Library/Caches/com.apple.mobile.installation.plist"];
NSDictionary *User = [dict valueForKey:@"User"];

//get first app version
NSDictionary *bundleID = [User valueForKey:appBundleID];
NSString *appVersion = [bundleID valueForKey:@"CFBundleVersion"];

//get second app version
NSDictionary *bundleID2 = [User valueForKey:appBundleID2];
NSString *appVer2 = [bundleID2 valueForKey:@"CFBundleVersion"];

[dict release];

if ([appVersion isEqualToString:appVer2]) {
     NSString *str1=[NSString stringWithFormat:@"Original Version: %@",appVersion];
     NSString *str2=[NSString stringWithFormat:@"2nd Version: %@",appVer2];
     NSString *msg=[NSString stringWithFormat:@"%@\n%@",str1,str2];
     UIAlertView* alertView = [[UIAlertView alloc]
           initWithTitle:@"Same Versions!" message:msg delegate:nil
       cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show]; 
}
else {
     NSString *str1=[NSString stringWithFormat:@"Original Version: %@",appVersion];
     NSString *str2=[NSString stringWithFormat:@"2nd Version: %@",appVer2];         
     NSString *msg=[NSString stringWithFormat:@"%@\n%@",str1,str2];
     UIAlertView* alertView = [[UIAlertView alloc]
         initWithTitle:@"Different Versions!" message:msg delegate:nil
            cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show]; 
}

The version of both apps is currently set to 2.11.8 这两个应用程序的版本当前设置为2.11.8

I am getting the following wrong result: 我得到以下错误结果: 结果错误

If i set the NSString manually: 如果我手动设置NSString:

NSString *appVersion =@"2.11.8";
NSString *appVer2 =@"2.11.8";

i get the correct desired result: 我得到正确的预期结果:

正确结果

I also tried other ways to compare the strings but the result was always the same, so i guess the problem is with fetching the values of the keys? 我还尝试了其他方法来比较字符串,但是结果始终相同,所以我猜问题出在获取键的值上? Any help is appreciated 任何帮助表示赞赏

I am so used to ARC that I am not 100% sure about the MRC rules anymore. 我已经习惯了ARC,因此我不再100%知道MRC规则。 But I assume that you either have to retain the values appVersion and appVer2 from the dictionary, or alternatively, postpone the [dict release] until after the values are no longer needed. 但是我假设您要么必须retain字典中的值appVersionappVer2 ,要么将[dict release]推迟到不再需要这些值之后。 Since you don't own the values fetched from the dictionary, they become invalid if the dictionary is released. 由于您不拥有从字典中获取的值,因此如果释放字典,它们将变为无效。

(This would not be a problem if you compile with ARC!) (如果使用ARC编译,这不会有问题!)

Remark: The designated method to get a value from a dictionary is objectForKey: . 备注:从字典中获取值的指定方法是objectForKey: valueForKey: works also in many cases, but can be different. valueForKey:在许多情况下也可以使用,但是可以有所不同。 It should only be used for Key-Value Coding magic. 它仅应用于键值编码魔术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM