【问题标题】:Convert CFStringRef to NSString将 CFStringRef 转换为 NSString
【发布时间】:2014-10-10 14:11:40
【问题描述】:

我正在尝试从主包中检索一些信息:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {    
    CFStringRef bundleVer = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
    NSString *appVersion = (__bridge NSString *)bundleVer;
}

我可以获得 CFStringRef(在调试中我可以看到与变量关联的正确值),但是当我尝试将其强制转换为 NSString 时,我的 appVersion 变量具有“null”值(之前它是 nil)。

我做错了什么?

我正在使用 ARC。

编辑:我的项目似乎有问题,即使使用简单的静态赋值也无法分配每个 NSString 对象,测试变量的值为(null)

NSString *test = @"";

【问题讨论】:

  • 你的 CFStringRef 的值是多少?我复制了你的代码,它完全适用于我的应用程序。
  • 在我的应用程序中也可以正常工作..
  • 可能与拼写错误的“bundle”有关。
  • 值是 34,因为它在我的捆绑文件中...分配后 appVersion 从 nil 变为 (null)

标签: objective-c macos cocoa


【解决方案1】:
  CFStringRef boundleVer = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
  NSString *appVersion = (__bridge NSString *)(boundleVer);

它在我这边工作,它正在返回“2”

【讨论】:

    【解决方案2】:

    我会使用该代码而不是您当前的代码来获取相同的信息:


    NSString *_shortVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    

    NSString *_version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    

    注意:我不确定您最终需要捆绑包中的哪些信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多