Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 704
u54r Avatar asked Apr 29 '26 01:04

u54r


1 Answers

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.

like image 60
akashivskyy Avatar answered May 01 '26 14:05

akashivskyy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!