ios - fatal error : Use of unimplemented initializer 'init()' for class Swift

标签 ios swift textview

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

但是在使用复制和粘贴时出现以下错误

Fatal error: Use of unimplemented initializer 'init()' for class 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
}

完整错误截图

有人对如何解决这个问题有任何建议吗?

最佳答案

在堆栈跟踪和控制台输出中,您可以看到 Objective-C 端尝试调用不带参数的初始化程序。

有人可能认为有一个提供了默认值参数,但那只能在 Swift 端工作,因为它没有暴露给 Objective-C 端。

因此,如果来自 Objective-C 背景的人可能会认为,初始化器可能是继承的。但 Swift 并非如此:

Initializer Inheritance and Overriding

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

请看这里:https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

解决方案

所以如果你像这样提供一个没有参数的初始化器:

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

那么当从 Objective-C 端调用时它也可以工作。

关于ios - fatal error : Use of unimplemented initializer 'init()' for class Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53402828/

相关文章:

android - 为什么部分文本位于屏幕后面?

ios - 在 Swift 中将整数转换为 NSData

c++ - 告诉 gnu-autotools 我新安装的库在哪里(使用 PKG_CONFIG?)

ios - SwiftUI ForEach 中的动态绑定(bind)字段

ios - 从 web 服务解析 JSON 数据时,没有返回数据

android - Ellipsize 对于具有任意最大高度的多行 TextView 无法正常工作

ios - 单击时我的 IBAction 不起作用

ios - 在 Realm Mobile Platform 中,如何调用 self.realm.objects 到 List<Item>

ios - 如何快速将 map 置于用户位置的中心?

Android textview 在包装时将单词分开