我跟随苹果的Docs在Swift中使用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'
从苹果的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.知道怎么解决这个问题吗?谢谢!
发布于 2014-09-04 19:19:55
首先,您必须给它一个显式的可选类型(使用?):
var currentText: CFAttributedStringRef? = ...然后你可以把它和零比较:
if currentText != nil {
// good to go
}您的代码目前正在编译,因为苹果还没有“快速”的CoreFoundation来返回正确注释的类型。
做好准备,在最终版本中,您的代码甚至不会编译,这迫使您使用可选类型。
https://stackoverflow.com/questions/25673392
复制相似问题