gpt4 book ai didi

ios - 在 Swift 中创建 PDF

转载 作者:搜寻专家 更新时间:2023-10-31 22:36:22 24 4
gpt4 key购买 nike

我正在关注 Apple's Docs在 Swift 中使用 Xcode6-Beta6 创建 PDF 文件

var currentText:CFAttributedStringRef = CFAttributedStringCreate(nil, textView.text as NSString, nil)

if (currentText) { // <-- This is the line XCode is not happy

// More code here

}

编译器抛出 Type 'CFAttributedStringRef' does not conform to protocol 'BooleanType' 错误

如果我使用 if(currentText != nil) 我会得到 'CFAttributedStringRef' is not convertible to 'UInt8'

来自 Apple 的 CFAttributedStringCreate 文档

Return Value
An attributed string that contains the characters from str and the attributes specified by attributes. The result is NULL if there was a problem in creating the attributed string. Ownership follows the Create Rule.

知道如何解决这个问题吗?谢谢!

最佳答案

首先你必须给它一个明确的可选类型(使用 ?):

var currentText: CFAttributedStringRef? = ...

然后你可以将它与 nil 进行比较:

if currentText != nil {
// good to go
}

您的代码目前可以编译,因为 Apple 尚未“快速化”CoreFoundation 以返回正确注释的类型。

请做好准备,在最终版本中您的代码甚至无法编译,迫使您使用可选类型。

关于ios - 在 Swift 中创建 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673392/

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