Ios 致命错误:使用框架时,无法将值从Swift类型桥接到Objective-C类型

Ios 致命错误:使用框架时,无法将值从Swift类型桥接到Objective-C类型,ios,objective-c,swift,Ios,Objective C,Swift,我正在使用以下项目,并已启用核心数据以保存UITextView中的文本 在保存文本和使用标记之前,一切正常,直到我开始更改viewdiload中的一些文本属性。我已经按照GitHub页面上的示例介绍了如何更改属性,但仍然存在错误 这是我正在做的一个例子 import UIKit import CoreData import MarkdownKit var textStorage: MarkdownTextStorage? var textView: MarkdownTextView! ove

我正在使用以下项目,并已启用核心数据以保存
UITextView
中的文本

在保存文本和使用标记之前,一切正常,直到我开始更改
viewdiload
中的一些文本属性。我已经按照GitHub页面上的示例介绍了如何更改属性,但仍然存在错误

这是我正在做的一个例子

import UIKit
import CoreData
import MarkdownKit

var textStorage: MarkdownTextStorage?
var textView: MarkdownTextView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false

    var attributes = MarkdownAttributes()

    let fontFloat = CGFloat(28)


    attributes.headerAttributes?.h6Attributes = [
        NSForegroundColorAttributeName: UIColor.grayColor(),
        NSFontAttributeName: UIFont(name: "OpenSans", size: 20)!
    ]

    attributes.defaultAttributes = [
        NSForegroundColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName: UIFont(name: "OpenSans", size: fontFloat)!
    ]

    attributes.strongAttributes = [
        NSForegroundColorAttributeName: UIColor.redColor()
    ]

    let textStorage = MarkdownTextStorage(attributes: attributes)


    do {
        textStorage.addHighlighter(try LinkHighlighter())
    } catch let error {
        fatalError("Error initializing LinkHighlighter: \(error)")
    }
    textStorage.addHighlighter(MarkdownStrikethroughHighlighter())
    textStorage.addHighlighter(MarkdownSuperscriptHighlighter())

    if let codeBlockAttributes = attributes.codeBlockAttributes {
        textStorage.addHighlighter(MarkdownFencedCodeHighlighter(attributes: codeBlockAttributes))
    }

    textView = MarkdownTextView(frame: CGRectZero, textStorage: textStorage)
    textView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(textView)

    let views = ["textView": textView]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-60-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
    NSLayoutConstraint.activateConstraints(constraints)


    textView.delegate = self

    userSettings()

}
有时这很好,
ViewController
会加载所有自定义属性,但有时会出现以下错误:

致命错误:在展开可选值时意外发现nil

以下是MarkdownAttributes结构如何在框架内设置属性

public var defaultAttributes: TextAttributes = [
    NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
]

public var h1Attributes: TextAttributes? = [
    NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
]
有人知道问题是什么以及如何解决吗

UIFont(name: "OpenSans", size: 20)!
这里是强制展开
UIFont
初始值设定项,即使它被定义为一个可失败的初始值设定项(
init?
)。您可能没有正确安装该字体,此调用返回的是
nil
,而不是
UIFont
实例

下面是一个如何防范
nil
字体的示例:

attributes.headerAttributes?.h6Attributes = [
    NSForegroundColorAttributeName: UIColor.grayColor()
]

if let font = UIFont(name: "OpenSans", size: 20) {
    attributes.headerAttributes?.h6Attributes[NSFontAttributeName] = font
} else {
    attributes.headerAttributes?.h6Attributes[NSFontAttributeName] = UIFont.systemFontOfSize(20.0)
}

我实际上已经正确安装了字体,我已经检查并更改了文本的其他标签。我确实删除了
因此我不强制展开,但是我得到了可选类型“UIFont”的内联错误
值未展开;你想用“!”吗或者“?”?
@JKSDEV用一种可能的解决方案更新了我的答案,以打开字体。如果找不到OpenSans,我会使用系统字体。