![](/img/trans.png)
[英]Parse/Swift fatal error: use of unimplemented initializer 'init()
[英]Fatal error: Use of unimplemented initializer 'init()' for class Swift
我正在使用 [MarkdownTextView][1] 將基本降價添加到UITextView
。 TextView
是MarkdownTextView
的子類。
但是,當使用復制和粘貼時,我收到以下錯誤
致命錯誤:對 MarkdownTextStorage 類使用未實現的初始化程序“init()”
這就是我在 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 並非如此:
初始化繼承和覆蓋
與 Objective-C 中的子類不同,Swift 子類默認不繼承其超類初始值設定項。
請參見此處: https : //docs.swift.org/swift-book/LanguageGuide/Initialization.html
解決方案
因此,如果您提供一個沒有參數的初始化程序,如下所示:
public override convenience init() {
self.init(attributes: MarkdownAttributes())
}
那么它在從 Objective-C 端調用時也能工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.