gpt4 book ai didi

objective-c - Xcode 在 NSAttributedString 上调用 RTFFromRange 时生成警告,documentAttributes 为 nil

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:38 25 4
gpt4 key购买 nike

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)。

关于objective-c - Xcode 在 NSAttributedString 上调用 RTFFromRange 时生成警告,documentAttributes 为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com