Fatal error: Use of unimplemented initializer 'init()' for class Swift

跟風遠走 提交于 2020-01-24 01:43:05

问题


I am using MarkdownTextView to add basic markdown to a UITextView. The TextView is a subclass of MarkdownTextView.

However when using copy and paste I get the following error

Fatal error: Use of unimplemented initializer 'init()' for class MarkdownTextStorage

This is how I use the TextStorage in my ViewController

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

I have used the following initialiser but still have no luck

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

Here's the full source code for the class

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
}

}

Full Error Screenshot

Does anyone have any suggestions on how to fix the issue ?


回答1:


In the stack trace and console output you can see that the Objective-C side tries to call the initializer without arguments.

One might think there is one supplied with default value parameter, but that would just work from Swift side, because it is not exposed to the Objective-C side.

So if coming from an Objective-C background one might think, that the intializer might be inherited. But that's not the case with Swift:

Initializer Inheritance and Overriding

Unlike subclasses in Objective-C, Swift subclasses do not inherit their superclass initializers by default.

see here: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

Solution

So if you supply an initializer without parameters like so:

public override convenience init() {
    self.init(attributes: MarkdownAttributes())
}

then it works also when called from Objective-C side.



来源:https://stackoverflow.com/questions/53402828/fatal-error-use-of-unimplemented-initializer-init-for-class-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!