u54r
u54r

Reputation: 1805

Create PDF in Swift

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!

Upvotes: 3

Views: 5341

Answers (1)

akashivskyy
akashivskyy

Reputation: 45170

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.

Upvotes: 3

Related Questions