Fatal error: Use of unimplemented initializer 'init()' for class Swift
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("(?:nn|\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 ?
ios swift textview
add a comment |
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("(?:nn|\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 ?
ios swift textview
How does yourMarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance ofMarkdownTextStorage
gets created. That is likely where your problem lies.
– Duncan C
Nov 21 at 0:06
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
The only code you posted that creates a MarkdownTextStorage object uses theMarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.
– Duncan C
Nov 21 at 2:37
add a comment |
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("(?:nn|\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 ?
ios swift textview
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("(?:nn|\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 ?
ios swift textview
ios swift textview
edited Nov 30 at 15:04
asked Nov 20 at 22:55
A.Roe
3562724
3562724
How does yourMarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance ofMarkdownTextStorage
gets created. That is likely where your problem lies.
– Duncan C
Nov 21 at 0:06
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
The only code you posted that creates a MarkdownTextStorage object uses theMarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.
– Duncan C
Nov 21 at 2:37
add a comment |
How does yourMarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance ofMarkdownTextStorage
gets created. That is likely where your problem lies.
– Duncan C
Nov 21 at 0:06
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
The only code you posted that creates a MarkdownTextStorage object uses theMarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.
– Duncan C
Nov 21 at 2:37
How does your
MarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance of MarkdownTextStorage
gets created. That is likely where your problem lies.– Duncan C
Nov 21 at 0:06
How does your
MarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance of MarkdownTextStorage
gets created. That is likely where your problem lies.– Duncan C
Nov 21 at 0:06
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
The only code you posted that creates a MarkdownTextStorage object uses the
MarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.– Duncan C
Nov 21 at 2:37
The only code you posted that creates a MarkdownTextStorage object uses the
MarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.– Duncan C
Nov 21 at 2:37
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402828%2ffatal-error-use-of-unimplemented-initializer-init-for-class-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 1 at 9:15
Stephan Schlecht
3,9101810
3,9101810
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402828%2ffatal-error-use-of-unimplemented-initializer-init-for-class-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How does your
MarkdownTextStorage
object relate to the code you posted? You show that class, but now an instance ofMarkdownTextStorage
gets created. That is likely where your problem lies.– Duncan C
Nov 21 at 0:06
@DuncanC Apologies, I have updated the question
– A.Roe
Nov 21 at 1:35
The only code you posted that creates a MarkdownTextStorage object uses the
MarkdownTextStorage.init(attributes:)
initializer, so that shouldn't be the cause of your crash. You should look at the stack trace in the crash log and figure out where the init call is coming from.– Duncan C
Nov 21 at 2:37