Dror Chen
Dror Chen

Reputation: 51

Swift: Initializer does not override a designated initializer from its superclass

Im trying to inherit a class I made that inherits NSObject, those are the snippets:

bPlayer.swift:

import UIKit
import Foundation
import QuartzCore

class bluetoothPlayer: player {

    override init (game: MultiGame) {
        super.init(game: game)

    }

}

Player.swift:

import UIKit
import Foundation
import QuartzCore

class player: NSObject {

    init (game: NSObject) {
        super.init()

    }

}

But I get the following error:

Initializer does not override a designated initializer from its superclass

If I delete the "override" keyword from the bluetoothPlayer init, the following error comes up instead:

Initializer 'init(game:)' with Objective-C selector 'initWithGame:' conflicts with initializer 'init(game:)' from superclass 'player' with the same Objective-C selector

Upvotes: 0

Views: 3585

Answers (1)

Arc676
Arc676

Reputation: 4465

That's because you didn't. You didn't override the function, you overloaded it. You made a new function (init) with the same name but different parameters. This doesn't count as overriding a function.

See this SO question (in Java, however). See this other SO question about overriding multiple functions in Swift.

This is because NSObjectMultiGame

You can override your init function by declaring it like so in bluetoothPlayer:

override init (game: NSObject) {
    super.init(game: game)
}

You can do a check to make sure that game is of type MultiGame to achieve the same effect as what you wrote though.

Upvotes: 2

Related Questions