2014-09-04 215 views
3

創建PDF我下面Apple's Docs斯威夫特創建使用Xcode6-Beta6 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'

從蘋果公司的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. 

任何想法如何解決這個問題?謝謝!

回答

3

首先,你必須給它一個明確的可選類型(使用?):

var currentText: CFAttributedStringRef? = ... 

然後,你可以把它比作零:

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

你的代碼編譯在這一刻,因爲Apple還沒有「swiftified」CoreFoundation來正確返回註釋類型。

準備在最終版本中代碼甚至不會編譯,迫使您使用可選類型。