在Swift中创建PDF [英] Create PDF in Swift

查看:176
本文介绍了在Swift中创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆