【问题标题】:Fatal error: Use of unimplemented initializer 'init()' for class Swift致命错误:对 Swift 类使用未实现的初始化程序“init()”
【发布时间】:2019-04-23 11:27:11
【问题描述】:

我正在使用 [MarkdownTextView][1] 将基本降价添加到 UITextViewTextViewMarkdownTextView 的子类。

但是,当使用复制和粘贴时,我收到以下错误

致命错误:对类使用未实现的初始化程序“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 的实例。这很可能是您的问题所在。
  • @DuncanC 抱歉,我已经更新了问题
  • 您发布的唯一创建 MarkdownTextStorage 对象的代码使用 MarkdownTextStorage.init(attributes:) 初始化程序,因此这不应该是您崩溃的原因。您应该查看崩溃日志中的堆栈跟踪,并找出 init 调用的来源。

标签: ios swift textview


【解决方案1】:

在堆栈跟踪和控制台输出中,您可以看到 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 端调用时它也可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多