Initializer Does Not Override a Designated Initializer from Its Superclass

Initializer does not override a designated initializer from its superclass

My solution is a quick fix, but I think is easier than what Apple purposes on the the Release Notes. For more information search for 19775924 http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_3/Xcode_6.3_beta_3_Release_Notes.pdf here. What Apple says is that you create an Objective-C file and extend it (having to add it to the header files and all) and it's on "Known Issues in Xcode 6.3 beta 3", so I think is easy to do what I did:

This is how I fixed it for UIButton:

class CustomButton : UIButton {
init() {
super.init(frame: CGRectZero)
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

And this is one of my ViewControllers (remove public if not needed):

public class GenericViewController: UIViewController {
public init() {
super.init(nibName: nil, bundle: nil)
}

required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

I don't use IB so I also have UIView, because I do separate the view from the viewController (remove public if not needed):

public class GenericMenuView: UIView {
public init() {
super.init(frame: CGRectZero)
}

public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

I need this specially in views because I have a setupViews method that I override in all subclasses that is called on the init. And using AutoLayout I don't need any frames (so I don't override the init with the frame parameter).

So it seems you have to drop override. Oh! and be sure to not call self.init() or the class is never initialized (and it crashes after some internal timeout).

Initializer does not override a designated initializer from its superclass, Swift 2.0

Since UISlider doesn't define or inherit init() as a designated initializer, you can't call it from your subclass init.

class CustomSlider: UISlider {

var sliderIdentifier: Int!

required init() {
super.init(frame: CGRect.zero)
sliderIdentifier = 0
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sliderIdentifier = 0
}

}

In this case, you could factor out the initialization of sliderIdentifier and just inherit all of UISliders initializers:

class CustomSlider: UISlider {

var sliderIdentifier: Int! = 0

}

For more information, read the section titled “Designated Initializers and Convenience Initializers” in The Swift Programming Language.

Initializer does not override a designed initializer from its superclass error in swift

You've got a few different errors here; let's deal with them one at at time.

Overriding a convenience initializer
Per the Swift documentation from Apple:

if you write a subclass initializer that matches a superclass convenience initializer, that superclass convenience initializer can never be called directly by your subclass, as per the rules described above in Initializer Delegation for Class Types. Therefore, your subclass is not (strictly speaking) providing an override of the superclass initializer. As a result, you do not write the override modifier when providing a matching implementation of a superclass convenience initializer.

So ditch the override keyword and you should be set. Speaking of sets…

Upgraded interfaces for touch methods in Swift 1.2
Paul Solt of iphonedev.tv covers this in Swift 1.2 fixes and breaks a few things: you should be excited!, and I recommend you read the whole post (not to mention the release notes that he links to), but the short of it is that NSSet has been replaced by a native Set type. As he says:

Fix: You'll need to update your method signature (i.e.: the entire first line) to the following:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

He also notes that there's no an anyObject() method on Set, so you're going to have to work around that with the interface provided.

Hope that helps!

Error message saying I am not overriding init from superclass even though I feel like I am

Ok so I needed to add the public keyword to the superclass init method to make this work:

override public init(frame: CGRect) {
super.init(frame: frame)
setUp()
}

How to declare a subclass designated initializer with more parameters than superclass?

This has nothing to do with your implementation of init(codImagen:String, respCorrecta:Int, codAyuda: String) (though that implementation is in fact wrong). It has to do with the fact that your superclass has adopted Codable.

Codable requires an implementation of init(from:). Your superclass inherits this through a protocol extension, so there's no problem about the fact that you have not supplied an implementation.

But the subclass is another story. By creating a designated initializer in the subclass, you have killed inheritance. Therefore, your subclass does not inherit the implementation of init(from:) from the superclass. Therefore you must supply it explicitly in the subclass:

class Pregunta: Codable {
var codImagen: String
var respCorrecta: Int
var respUsuario = -1

init(codImagen:String, respCorrecta:Int){
self.codImagen = codImagen
self.respCorrecta = respCorrecta
}
}

class PregRev: Pregunta {
var codAyuda: String
enum CodingKeys : String, CodingKey {
case codAyuda
}
init(codImagen:String, respCorrecta:Int, codAyuda: String){
self.codAyuda = codAyuda
super.init(codImagen: codImagen, respCorrecta: respCorrecta)
}
required init(from decoder: Decoder) throws {
let con = try decoder.container(keyedBy: CodingKeys.self)
self.codAyuda = try con.decode(String.self, forKey: .codAyuda)
try super.init(from:decoder)
}
}

Build error when trying to override an initializer in Xcode 6.3 Beta 3

A designated initializer of a subclass needs to call the designated initializer of Superclass. A convenience initializer can only call another convenience initializer or a designated initializer of that class.

init() is a convenience initializer for UIView, if you subclass UIView you should call its designated initializer which is init(frame: frame)

override init() {
super.init(frame: frame)
// Some init logic ...
}

EDIT: Apparently in Beta 3, UIView doesn't have convenience initializer called as init, so you need to remove the override keyword too, now this is a designated initializer so you need to call superclass's designated initializer

init() {
super.init(frame: frame)
// Some init logic ...
}

EDIT: Although this works but I think a better way to write this would be:

convenience init() {
self.init(frame:CGRectZero)
}

Source:Swift documentation

Rule 1 A designated initializer must call a designated initializer
from its immediate superclass.

Rule 2 A convenience initializer must call another initializer from
the same class.

Rule 3 A convenience initializer must ultimately call a designated
initializer.



Related Topics



Leave a reply



Submit