在Swift中创建PDF

编程入门 行业动态 更新时间:2024-10-25 23:36:59
本文介绍了在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

本文发布于:2023-10-31 01:49:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   PDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!