在Swift中创buildPDF
我正在关注苹果的Docs ,在Swift中使用Xcode6-Beta6创build一个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'
从苹果的文件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.
任何想法如何解决这个问题? 谢谢!
首先你必须给它一个明确的可选types(使用?
):
var currentText: CFAttributedStringRef? = ...
那么你可以把它比作零:
if currentText != nil { // good to go }
你的代码目前正在编译,因为苹果还没有“swiftified”CoreFoundation返回正确的注释types。
做好准备,在最终版本中,你的代码甚至不会编译,迫使你使用可选types。