我正在尝试比较CFBundleVersion
com.apple.mobile.installation.plist中2个应用程序的键,其中包括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];
}
这两个应用程序的版本当前设置为2.11.8
我得到以下错误结果:
如果我手动设置NSString:
NSString *appVersion =@"2.11.8";
NSString *appVer2 =@"2.11.8";
我得到正确的预期结果:
我还尝试了其他方法来比较字符串,但结果始终相同,所以我猜问题出在获取键的值上?任何帮助表示赞赏
我已经习惯于ARC,以至于我不再100%知道MRC规则。但是我假设您要么必须从字典中retain
获取值appVersion
,要么将其appVer2
推迟[dict release]
到不再需要这些值之后。由于您不拥有从字典中获取的值,因此如果释放字典,它们将变为无效。
(如果使用ARC编译,这不会有问题!)
备注:从字典中获取值的指定方法是objectForKey:
。valueForKey:
在许多情况下也可以使用,但可以有所不同。它仅应用于键值编码魔术。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句