Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 致命错误:使用未实现的初始值设定项';init()';斯威夫特一班_Ios_Swift_Textview - Fatal编程技术网

Ios 致命错误:使用未实现的初始值设定项';init()';斯威夫特一班

Ios 致命错误:使用未实现的初始值设定项';init()';斯威夫特一班,ios,swift,textview,Ios,Swift,Textview,我正在使用[MarkdownTextView][1]将基本标记添加到UITextView。TextView是MarkdownTextView的子类 但是,当使用复制和粘贴时,我得到以下错误 致命错误:对类使用未实现的初始值设定项“init()” MarkdownTextStorage 这是我在ViewController中使用文本存储的方式 let fonty = UIFont(name: font, size: fsize) attributes.defaultAttributes[

我正在使用[MarkdownTextView][1]将基本标记添加到
UITextView
TextView
MarkdownTextView
的子类

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

致命错误:对类使用未实现的初始值设定项“init()” MarkdownTextStorage

这是我在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))
 }
我使用了下面的初始化器,但仍然没有运气

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:HighlighterTextStorage{
fileprivate let属性:MarkdownAttributes
//标记:初始化
/**
创建接收器的新实例。
:param:用于设置文本样式的属性。
:返回:“MarkdownTextStorage”的初始化实例`
*/
public init(属性:MarkdownAttributes=MarkdownAttributes()){
self.attributes=属性
super.init()
commonInit()
如果让headerAttributes=attributes.headerAttributes{
addHighlighter(MarkdownHeaderHighlighter(属性:headerAttributes))
}
addHighlighter(标记下行链路Highlighter())
addHighlighter(MarkdownListHighlighter(markerPattern:“[*+-]”,属性:attributes.unorderedListAttributes,itemAttributes:attributes.unorderedListItemAttributes))
addHighlighter(MarkdownListHighlighter(markerPattern:“\\d+[.]”,属性:attributes.orderedListAttributes,itemAttributes:attributes.orderedListItemAttributes))
//来自markdown.pl v1.0.1
//代码块
addPattern((?:\n\n |\\A)((?:(?:[{4}}\t)。*\n+)((?=^[]{0,4}\\S)|\\Z)”,attributes.codeBlockAttributes)
//块引号
addPattern((?:^[\t]*>[\t]?.+\n(+\n)*\n*)+,attributes.blockQuoteAttributes)
//Se文本样式标题
//H1
addPattern(“^(?:.+)[\t]*\n=+[\t]*\n+”,attributes.headerAttributes?.h1Attributes)
//氢
addPattern(“^(?:.+)[\t]*\n-+[\t]*\n+”,attributes.headerAttributes?.h2Attributes)
//重点

addPattern(“(\\*\\\)\”(?=\\S)(.+?)(?在堆栈跟踪和控制台输出中,您可以看到Objective-C端尝试调用没有参数的初始值设定项

有人可能会认为有一个提供了默认值参数,但这只在Swift端起作用,因为它不暴露于Objective-C端

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

初始值设定项继承和重写

与Objective-C中的子类不同,Swift子类默认情况下不继承其超类初始值设定项

请看这里:

解决方案

因此,如果您提供的初始值设定项没有如下参数:

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

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

您的
MarkdownTextStorage
对象与您发布的代码有什么关系?您显示了该类,但现在创建了
MarkdownTextStorage
的一个实例。这可能是您的问题所在。@Duncan抱歉,我已经更新了问题您发布的唯一代码at创建MarkdownTextStorage对象时使用
MarkdownTextStorage.init(attributes:)
初始值设定项,因此这不应该是崩溃的原因。您应该查看崩溃日志中的堆栈跟踪,并找出init调用的来源。