0

I am trying to create a UIButton by trying to initialise in a viewController as shown below. However, I am getting cannot convert value of type () -> _ to specified type UIButton error which I don't have idea how I stuck into.

let loginRegisterButton : UIButton = {
    let button = UIButton(type: .System)
    button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
    button.setTitle("Register", forState: .Normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}

This is the sample code and I have used a UIColor extension and I have attached the screenshot of error herewith.

enter image description here

As far as I know, this is not a syntax error, but I have no idea what I am missing.

1 Answer 1

2

you forgot to add

()

at the end Use Below code ,

let loginRegisterButton : UIButton = {
    let button = UIButton(type: .System)
    button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
    button.setTitle("Register", forState: .Normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

What you are missing is mentioned in the error message, that you are assigning closure to UIButton instance. By adding () this will create instance of UIButton.

Sign up to request clarification or add additional context in comments.

1 Comment

Please also support your answer with explanation that way it would be of a great help to other learners.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.