Blazej SLEBODA
Blazej SLEBODA

Reputation: 9915

constant ... inferred to have type '()?', which may be unexpected

When I am chaining a methods on the following object

let developerButton = UIButton(type: .system).titleLabel?.text = "developer"

I get the message:

Constant 'developerButton' inferred to have type '()?', which may be unexpected

I don't get it. Any help ?

Upvotes: 1

Views: 4306

Answers (2)

Sulthan
Sulthan

Reputation: 130082

Your developerButton is the result of the text assignment text = "developer", which returns Void (nothing, ()). The assignment is conditional and that's why you are getting Void? (or ()?).

Split your two assignments correctly.

let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"

Also note that you should use setTitle instead of setting title text directly:

let developerButton = UIButton(type: .system)
developerButton.setTitle("developer", for: .normal)

Upvotes: 5

a.afanasiev
a.afanasiev

Reputation: 249

Try this:

let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"

Upvotes: 0

Related Questions