2010-04-20 65 views
4

在这里,我粘贴我的代码,我想从我的test-Info.plist中检索Bundle版本。如何从iPhone中的项目test-Info.plist中检索标签中的Bundle版本?

@interface testViewController : UIViewController { 
    UILabel *label; 
} 
@property(nonatomic,retain) IBOutlet UILabel *label; 

@end 

@implementation testViewController 

@synthesize label; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"test-info" ofType:@"plist"]; 

    NSString *versionString = [NSString stringWithFormat:@"v%d", [plistData objectForKey:@"Bundle version"]]; 

    label.text = versionString; 
} 

@end 

但我仍得到了空值在那里我错了

+0

你真的应该只发布与你的问题相关的那部分代码 - 很难浏览上百行代码来找到它... – Vladimir 2010-04-20 13:48:58

+0

另外检查这个问题:http://stackoverflow.com/questions/2657477/how-to-check-bundle-version-for-our-application-by-programming/2657500 – Vladimir 2010-04-20 13:53:58

+0

可能的重复[如何阅读从PList的捆绑版本?](http://stackoverflow.com/questions/2244985/how-to-read-the-bundle-version-from-plist) – 2016-12-26 15:36:19

回答

1

尝试通过这样获取的plist路径:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test-info" ofType:@"plist"]; 
+0

仍然即时获得空值在我的标签 – AmanGupta007 2010-04-20 14:30:11

+0

'[plistData objectForKey :@“Bundle version”]'将返回一个'NSNumber'。在格式字符串中使用'@“v%@”'来转换NSNumber(您应该始终在格式化字符串中使用'%@'作为Obj-C对象),或者保留为'@“v%d”'和使用'[[plistData objectForKey:@“Bundle version”] intValue]'将'NSNumber'转换为'int'。 – 2010-04-23 07:17:07

36

下面一行将检索版本供您

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 
+2

这是真正的答案..不是接受的答案 – Krishnabhadra 2012-09-13 11:22:20

+7

也值得注意的是:“Bundle Version”或“CFBundleVersion”等同于应用程序的内部版本号。如果您想要实际的“x.y”字符串,请改为使用“CFBundleShortVersionString”。 – jemmons 2013-03-13 02:36:12

相关问题