swift – RTF文件到属性字符串

swift – RTF文件到属性字符串,第1张

概述我正在尝试将RTF文件内容读取为属性字符串,但attributesText为零.为什么? if let fileURL = NSBundle.mainBundle().URLForResource(filename, withExtension: "rtf") { var error: NSError? if let attributedText = NSAttributedStr 我正在尝试将RTF文件内容读取为属性字符串,但attributesText为零.为什么?

if let fileURL = NSBundle.mainBundle().URLForResource(filename,withExtension: "rtf") {    var error: NSError?    if let attributedText = NSAttributedString(fileURL: fileURL,options: [NSdocumentTypedocumentAttribute:NSRTFDTextdocumentType],documentAttributes: nil,error: &error){        textVIEw.attributedText = attributedText    }}

更新:我将代码更改为:

if let fileURL = NSBundle.mainBundle().URLForResource(filename,withExtension: "rtf") {    var error: NSError?    let attributedText = NSAttributedString(fileURL: fileURL,options: [NSdocumentTypedocumentAttribute:NSRTFTextdocumentType],error: &error)        println(error?.localizedDescription)        textVIEw.attributedText = attributedText}

现在textVIEw.attributedText = attributedText上出现崩溃:致命错误:在展开Optional值时意外发现nil.我在调试器中看到,attributesText是非零的,包含带有文件属性的文本.

解决方法 而不是在调试器中查看 *** 作是否有效/失败,您可以更好地编写代码来适当地处理失败:

if let attributedText = NSAttributedString(fileURL: fileURL,error: &error) {    textVIEw.attributedText = attributedText}else if let error = error {                    println(error.localizedDescription)}

斯威夫特4

do {    let attributedString = try NSAttributedString(url: fileURL,options: [NSAttributedString.documentReadingOptionKey.documentType: NSAttributedString.documentType.rtf],documentAttributes: nil)} catch {    print("\(error.localizedDescription)")}
总结

以上是内存溢出为你收集整理的swift – RTF文件到属性字符串全部内容,希望文章能够帮你解决swift – RTF文件到属性字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1007183.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存