在Swift中创建PDF

时间:2014-09-04 19:16:11

标签: ios swift pdf-generation

我跟随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.

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

1 个答案:

答案 0 :(得分:3)

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

var currentText: CFAttributedStringRef? = ...

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

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

您的代码目前正在编译,因为Apple尚未“彻底改变”CoreFoundation以返回正确注释的类型。

请准备好在最终版本中,您的代码甚至不会编译,强制您使用可选类型。