Hello! I Want to Create Constructor for My Class and I Have an Error: Super.Init Isn't Called on All Paths Before Returning from Initializer Swift

Hello! I want to create constructor for my class and I have an error: Super.init isn't called on all paths before returning from initializer swift

Add

super.init(frame: CGRectZero)

in your init, class need to init it's super class

Swift: 'super.init' isn't called on all paths before returning from initializer?

Since you inherit from UIViewController, you should call super.init right after you set the variables in your init function

When you inherit a class and implement a new init function or override its own init function you should (almost) always call super.init. Let's take your example, you inherited from UIViewController. UIViewController has a few init functions that you can use to initialize a view controller. if you don't call super.init, all the code inside those functions will not get called and possibly the view controller won't get initialized.

Anyway, this piece of code should work for you:

class ViewController: UIViewController {

var button: UIButton?

init(button: UIButton) {
self.button = button
super.init(nibName: nil, bundle: nil)
}

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

}

Use NSProxy in Swift 4.1

NSProxy is a abstract class. Apple docs about NSProxy says "An abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet".

The docs about abstract class of wikipedia says:

In a language that supports inheritance, an abstract class, or abstract base class (ABC), is a class that cannot be instantiated because it is either labeled as abstract or it simply specifies abstract methods (or virtual methods).

Calling super.init() for abstract class is wrong.
In second class you are not calling super.init() for abstract class but of WorkingProxyBaseClass which is a concrete class. In Objective c you have not called [super init] hence code is working.

How to override Init() in the subclass in Swift 3

So you have two ways to handle your case:

1 Create an own initializer for your subclass, call the super initializer and after that initialize the weapon property like this

class Goblin: NonPlayerCharacter {
var weapon : Int = 0

init(health: Int, power: Int, weapon: Int) {
super.init(health: health, power: power)
self.weapon = weapon
}

override func attack() -> String {
return "attack from Goblin"
}
}

Then you are able to create a Goblin like this:

var goblin1 = Goblin(health: 30, power: 20, weapon: 50)

2 Create a convenience initializer for your subclass to be able to decide if you want to call only the initializer from the parent class (with setting health and power) or the convenience one (with setting health, power and weapon) like this:

class Goblin: NonPlayerCharacter {
var weapon : Int = 0

convenience init(health: Int, power: Int, weapon: Int) {
self.init(health: health, power: power)
self.weapon = weapon
}

override func attack() -> String {
return "attack from Goblin"
}
}

Then you are able to create a Goblin like this:

var goblin2 = Goblin(health: 30, power: 20, weapon: 50)

or like this:

var goblin3 = Goblin(health: 30, power: 20)

Further readings here and here

Using a dispatch_once singleton model in Swift

tl;dr: Use the class constant approach if you are using Swift 1.2 or above and the nested struct approach if you need to support earlier versions.

From my experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety.

Class constant

class Singleton  {
static let sharedInstance = Singleton()
}

This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of let. This is now officially recommended way to instantiate a singleton.

Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.

Nested struct

class Singleton {
class var sharedInstance: Singleton {
struct Static {
static let instance: Singleton = Singleton()
}
return Static.instance
}
}

Here we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions.

dispatch_once

The traditional Objective-C approach ported to Swift. I'm fairly certain there's no advantage over the nested struct approach but I'm putting it here anyway as I find the differences in syntax interesting.

class Singleton {
class var sharedInstance: Singleton {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: Singleton? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = Singleton()
}
return Static.instance!
}
}

See this GitHub project for unit tests.



Related Topics



Leave a reply



Submit