Xcode 在对 documentAttributes 为 nil 的 NSAttributedString 上调用 RTFFromRange 时生成警告

Xcode generating warning when invoking RTFFromRange on NSAttributedString with nil for documentAttributes

Xcode 7 正在为如下所示的代码生成一堆警告:

NSAttributedString *anEntry = ...
someData = [anEntry RTFFromRange: NSMakeRange(0,[anEntry length]) documentAttributes: nil];

警告是:

Null argument passed to a callee that requires a non-null argument

Apple 的文档有点混乱(在我看来好像有编辑错误),但是当没有文档属性(没有文档属性)时,为 documentAttributes 参数传递 nil 似乎是可以接受的就我而言)。

我正在寻找解决此警告的方法。我可以创建一个(基本上)空的字典来作为 documentAttributes 传递,但这在我的代码中经常发生,我不想用额外的代码把它弄得乱七八糟。

我错过了真正的问题吗?文档关于 nil 是否可以接受是错误的,我真的应该为 documentAttributes 传递一些东西吗?

有人遇到过这个问题并找到了解决方案吗?

编辑:根据下面的答案,我最终选择了:

NSAttributedString *anEntry = ...
someData = [anEntry RTFFromRange: NSMakeRange(0,[anEntry length]) documentAttributes: @{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType}];

我相信这是正确的,它解决了警告。

从 Xcode 7 开始,Objective-C 允许您指定参数是否可以是 nil。对于NSAttributedString RTFFromRange:documentAttributes:方法,两个参数都不允许是nil。所以编译器现在告诉你不能将 nil 传递给不允许 nil.

的参数

您是对的,docAttributes 参数的文档令人困惑。它似乎只是部分更新。尝试传递一个空字典。

将代码更改为:

NSAttributedString *anEntry = ...
someData = [anEntry RTFFromRange: NSMakeRange(0,[anEntry length]) documentAttributes: @{}];

这将传递一个空字典而不是 nil

有关可空参数的示例,请参阅 NSAttributedString dataFromRange:documentAttributes:error: 文档(error 参数可以是 nil)。