致命错误:Swift类使用未实现的初始化器'init()'

3
我正在使用[MarkdownTextView][1]将基本的markdown添加到UITextView中。 TextViewMarkdownTextView的子类。
然而,当我使用复制和粘贴时,会出现以下错误:

致命错误:使用未实现的初始化程序“init()”为类MarkdownTextStorage

这是我在ViewController中使用TextStorage的方法。
let fonty = UIFont(name: font, size: fsize)
    
attributes.defaultAttributes[NSFontAttributeName] = fonty
attributes.orderedListAttributes?[NSFontAttributeName] = fonty
attributes.orderedListItemAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListItemAttributes?[NSFontAttributeName] = fonty

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))
 }

我使用了以下初始化程序,但仍然没有成功。
required public init?(coder aDecoder: NSCoder) {
    attributes = MarkdownAttributes()
    super.init(coder: aDecoder)
    commonInit()
}

这是该类的完整源代码。
open class MarkdownTextStorage: HighlighterTextStorage {

fileprivate let attributes: MarkdownAttributes

// MARK: Initialization

/**
Creates a new instance of the receiver.

:param: attributes Attributes used to style the text.

:returns: An initialized instance of `MarkdownTextStorage`
*/
public init(attributes: MarkdownAttributes = MarkdownAttributes()) {
    self.attributes = attributes
    super.init()
    commonInit()
    
    if let headerAttributes = attributes.headerAttributes {
        addHighlighter(MarkdownHeaderHighlighter(attributes: headerAttributes))
    }
    addHighlighter(MarkdownLinkHighlighter())
    addHighlighter(MarkdownListHighlighter(markerPattern: "[*+-]", attributes: attributes.unorderedListAttributes, itemAttributes: attributes.unorderedListItemAttributes))
    addHighlighter(MarkdownListHighlighter(markerPattern: "\\d+[.]", attributes: attributes.orderedListAttributes, itemAttributes: attributes.orderedListItemAttributes))
    
    // From markdown.pl v1.0.1 <http://daringfireball.net/projects/markdown/>
    
    // Code blocks
    addPattern("(?:\n\n|\\A)((?:(?:[ ]{4}|\t).*\n+)+)((?=^[ ]{0,4}\\S)|\\Z)", attributes.codeBlockAttributes)
    
    // Block quotes
    addPattern("(?:^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+", attributes.blockQuoteAttributes)
    
    // Se-text style headers
    // H1
    addPattern("^(?:.+)[ \t]*\n=+[ \t]*\n+", attributes.headerAttributes?.h1Attributes)
    
    // H2
    addPattern("^(?:.+)[ \t]*\n-+[ \t]*\n+", attributes.headerAttributes?.h2Attributes)
    
    // Emphasis
    addPattern("(\\*|_)(?=\\S)(.+?)(?<=\\S)\\1", attributesForTraits(.traitItalic, attributes.emphasisAttributes))
    
    // Strong
    addPattern("(\\*\\*|__)(?=\\S)(?:.+?[*_]*)(?<=\\S)\\1", attributesForTraits(.traitBold, attributes.strongAttributes))
    
    // Inline code
    addPattern("(`+)(?:.+?)(?<!`)\\1(?!`)", attributes.inlineCodeAttributes)
}

required public init?(coder aDecoder: NSCoder) {
    attributes = MarkdownAttributes()
    super.init(coder: aDecoder)
    commonInit()
}

fileprivate func commonInit() {
    defaultAttributes = attributes.defaultAttributes
}

// MARK: Helpers

fileprivate func addPattern(_ pattern: String, _ attributes: TextAttributes?) {
    if let attributes = attributes {
        let highlighter = RegularExpressionHighlighter(regularExpression: regexFromPattern(pattern), attributes: attributes)
        addHighlighter(highlighter)
    }
}

private func attributesForTraits(_ traits: UIFontDescriptorSymbolicTraits, _ attributes: TextAttributes?) -> TextAttributes? {
    var attributes = attributes
    if let defaultFont = defaultAttributes[NSFontAttributeName] as? UIFont , attributes == nil {
        attributes = [
            NSFontAttributeName: fontWithTraits(traits, font: defaultFont)
        ]
    }
    return attributes
}

}

完整错误截图

有人有关于如何解决这个问题的建议吗?


你的 MarkdownTextStorage 对象与你发布的代码有什么关系?你展示了那个类,但现在创建了一个 MarkdownTextStorage 的实例。这很可能是你问题出现的地方。 - Duncan C
@DuncanC 抱歉,我已更新问题。 - A.Roe
你发布的唯一创建MarkdownTextStorage对象的代码使用了MarkdownTextStorage.init(attributes:)初始化器,因此这不应该是导致崩溃的原因。你应该查看崩溃日志中的堆栈跟踪,并找出init调用来自哪里。 - Duncan C
1个回答

7
在堆栈跟踪和控制台输出中,您可以看到Objective-C侧试图调用没有参数的初始化程序。可能有人认为在提供带有默认值参数的初始化参数,但这只适用于Swift方面,因为它没有暴露给Objective-C方面。因此,如果来自Objective-C背景,可能会认为可以继承初始化程序。但在Swift中并非如此:

初始化程序继承和重载

与Objective-C中的子类不同,Swift子类默认情况下不会继承其超类初始化程序。

请参见此处: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html 解决方案 因此,如果您提供了一个没有参数的初始化程序,像这样:
public override convenience init() {
    self.init(attributes: MarkdownAttributes())
}

这样做的话,即使从Objective-C端调用,它也可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接