我最近开始使用Xcode 7,并得到了似乎是一个常见的警告:
Null passed to a callee that requires a non-null argument
Run Code Online (Sandbox Code Playgroud)
我理解它告诉我的是什么,但我不确定针对我的特定问题的正确解决方案是什么.这是发生警告的行:
NSTextTab *tab = [[NSTextTab alloc]
initWithTextAlignment:NSTextAlignmentLeft
location:10.0f
options:nil];
Run Code Online (Sandbox Code Playgroud)
现在,在他的"编程iOS 8"一书(在GitHub上发布)中查看Matt Neuberg的例子,我看到以下内容:
let s = "Onions\t$2.34\nPeppers\t$15.2\n"
let mas = NSMutableAttributedString(string:s, attributes:[
// lines omitted...
let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
let tab = NSTextTab(textAlignment:.Right, location:170, options:[NSTabColumnTerminatorsAttributeName:terms])
// lines omitted
self.tv.attributedText = mas
Run Code Online (Sandbox Code Playgroud)
据我所知,这是设置文本,以便字符串中的小数点是对齐的.大.有用.不是我需要的.我只是试图让左侧的标签给出一个特定且一致的缩进.
为了"修复"我的代码(即,让警告消失),我已将代码更改为:
NSTextTab *tab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:10.0f options:[NSDictionary dictionary]];
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但感觉就像一个超级笨拙的解决方案.我的理解是NSTextTab错误的吗?解决这个问题的正确方法是什么?
小智 5
initWithTextAlignment:location:options: 文档中列出的唯一选项是 NSTabColumnTerminatorsAttributeName 并且它是可选的。
options: 参数通过使用 NS_ASSUME_NONNULL_BEGIN/END 宏在 NSParagraphStyle.h 头文件中标记为非空。
结合这两个事实,传递空 NSDictionary 的解决方案是解决编译器警告的正确方法。
| 归档时间: |
|
| 查看次数: |
2139 次 |
| 最近记录: |