Create PDF in Swift

橙三吉。 提交于 2019-12-01 06:05:00

问题


I am following Apple's Docs to create a PDF file using Xcode6-Beta6 in Swift

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

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

   // More code here

}

Compiler throws Type 'CFAttributedStringRef' does not conform to protocol 'BooleanType' error

If I use if(currentText != nil) I get 'CFAttributedStringRef' is not convertible to 'UInt8'

From Apple's Docs for 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.

Any idea how to resolve this? Thanks!


回答1:


First you have to give it an explicit optional type (using the ?):

var currentText: CFAttributedStringRef? = ...

Then you can compare it to nil:

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

Your code compiles at the moment, because Apple hasn't yet "swiftified" CoreFoundation to return properly annotated types.

Be prepared that in the final release your code will not even compile, forcing you to use the optional type.



来源:https://stackoverflow.com/questions/25673392/create-pdf-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!