iphone - 返回强制转换为ARC下CFStringRef的NSString * -如何摆脱分析器警告?

标签 iphone objective-c ios automatic-ref-counting core-foundation

这是我的功能:

CFStringRef nameWithType (someEnum type) {
  NSString* r;
  switch (type) {
    case type1:
      r=@"type1";
      break;
    case type2:
      r=@"type2";
      break;
    case type3:
      r=@"type3";
      break;
  }
  return (__bridge CFStringRef)r;  // analyzer warns: Address of stack memory associated with local variable 'r' returned to caller.
}

最佳答案

实际上,有一种“传统方式”(从出现NSObject之前的时间开始)不使用NSString文字,而是像这样使用CFSTR宏:

CFStringRef nameWithType2(someEnum type){
    CFStringRef string = NULL;
    switch (type) {
        case type1:
            string = CFSTR("type1");
            break;
        case type2:
            string = CFSTR("type2");
            break;
        case type3:
            string = CFSTR("type3");
            break;
    }
    return string;
}
CFSTR(c_string)是创建CFStringRef的最短方法,比(__bridge CFStringRef)@"NSString"短得多

另外,如果有人看到此代码,您也会因了解CFSTR而获得较高的信誉。创建NSString文字,然后将其转换为CFStringRef并需要添加ARC内存管理所有权转移标记....而不是立即创建CFStringRef,这有点像N00B。

关于iphone - 返回强制转换为ARC下CFStringRef的NSString * -如何摆脱分析器警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870519/

相关文章:

objective-c - 为什么在这种情况下 respondsToSelector 对我不起作用?

objective-c - 转到另一个函数中的标签

iphone - 我们在ios开发中必须遵循的编码标准有哪些

iphone - 在 Objective-c 中实现协议(protocol)方法

iphone - 在 UITabBarController 和 UIViewController 之间切换(iOS Xcode)

ios - 为什么导入 ViewController 会导致 Xcode 无法找到另一个 ViewController?

objective-c - 防止父 View UIGestureRecongnizer 在 UITableVIew subview 中执行

ios - 一个 Storyboard多个 viewController 或多个 Storyboard每个都有一个 viewController 哪个更可取?

ios - 如何打开/关闭 iPhone 相机闪光灯?

iphone - [__NSArrayM getFileSystemRepresentation :maxLength:]: unrecognized selector sent to instance