我正在关注 Apple的文档在Swift中使用Xcode6-Beta6创建PDF文件
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 }编译器抛出Type 'CFAttributedStringRef' does not conform to protocol 'BooleanType'错误
如果我使用if(currentText != nil),我会得到'CFAttributedStringRef' is not convertible to 'UInt8'
If I use if(currentText != nil) I get 'CFAttributedStringRef' is not convertible to 'UInt8'
摘自Apple文档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!
推荐答案首先,您必须给它一个明确的可选类型(使用?):
First you have to give it an explicit optional type (using the ?):
var currentText: CFAttributedStringRef? = ...然后您可以将其与nil进行比较:
Then you can compare it to nil:
if currentText != nil { // good to go }
您的代码正在编译,因为Apple尚未简化" CoreFoundation以返回正确注释的类型.
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.
更多推荐
在Swift中创建PDF
发布评论