Create PDF in Swift

I am following Apple Docs to generate PDF 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

}

      

The compiler throws an 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 Docs to 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 solve this? Thank!

+3


source to share


1 answer


You must first give it an explicit optional type (using ?

):

var currentText: CFAttributedStringRef? = ...

      

Then you can compare it to nil:

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

      




Your code is compiling at this point because Apple has not yet "tweaked" CoreFoundation to return correctly annotated types.

Expect your code to not even compile in the final version, forcing you to use an optional type.

+3


source







All Articles