4thSpace
4thSpace

Reputation: 44352

Result of initializer not used?

Why do I get the message below when using the following:

MyClass.init(5)

Class definition:

class MyClass: NSObject {
private static var aNumber = 0

    init(myNumber: Int) {
        MyClass.aNumber = MyNumber
    }
...
}

Result of initializer not used

Upvotes: 0

Views: 144

Answers (1)

Sulthan
Sulthan

Reputation: 130191

You can remove the warning easily, e.g.

_ = MyClass(5)

However, you have a design problem. Why would you create an instance that you then discard right away?

Instead, make the setter public:

static var aNumber = 0

and just set

MyClass.aNumber = 5

or make it a static method:

static func setNumber(_ number: Int) {
    aNumber = number
}

and call

MyClass.setNumber(5)

Upvotes: 2

Related Questions