0

I have two classes, Model and ViewController. I have called a method in Model from ViewController on completion of which I need to execute the closure. This is What I have done:

Model.swift

typealias LoginCompletionBlock = () -> Void

func registerUser(username : String, emailID email : String, password userPassword : String, profileImage picture : UIImage, registrationMethod method : String, onCompletion completion : LoginCompletionBlock)
{
    //Necessary code for an async request
}

// Delegate for getting the registration details
func registrationSucceededForUser(userID : String, withAccessToken token : String)
{
    LoginCompletionBlock() // Error 'LoginCompletionBlock' is not constructible with '()'
}

And in ViewController.swift, I have called the function like this:

@IBAction func signUp(sender: UIButton)
{
    model.registerUser(usernameTextField.text, emailID: emailTextField.text, password: passwordTextField.text, profileImage: profileImageView.image!, registrationMethod: "normal", onCompletion:{
        () in
        //Perform actions after login
    }) //Error 'Bool' is not a subtype of 'Void'
}

I am just getting started with swift. Can anyone please guide me how to use the closures properly and how can I avoid this error. I need to pass a Bool as a parameter in the closure and no return type. I haven't included the Bool in the code since I was just trying to learn how to use closure.

1 Answer 1

1

If you need to pass a bool into the closure, you have to change the typealias from () -> Void to Bool -> Void. In addition, your registrationSucceededForUser function will need to be changed so the callback is passed in as a parameter. Right now, you're "invoking" a function signature, not an actual function.

Also, some line breaks in the registerUser function signature will go a long way for readability.

typealias LoginCompletionBlock = Bool -> Void
model.registerUser(usernameTextField.text, emailID: emailTextField.text,
    password: passwordTextField.text, profileImage: profileImageView.image!,
    registrationMethod: "normal", onCompletion: {
         success in
        //Perform actions after login
    })
}

edit: I've added the specific modifications I might make to the code. More information may be needed to actually understand the root of the type error. Note that if the body of your inline closure consists of one statement, it can be inferred to be the return value, and you may need to add newline and an empty return statement to satisfy the Void return type.

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

6 Comments

Sorry, didn't get you. Can you please show the correct code snippet?
I've added an example. "success" in this case is an argument to the function, implicitly typed as a Bool.
Ok, let me try this :)
Seems like the error with function call disappeared, but I am still confused about how to invoke the closure on successful registration.
Are you trying to figure out how to implement registrationSucceededForUser? Without knowing much about what's really happening here, I would tend to assume that your LoginCompletionBlock will be called in the background thread with a value indicating whether the login completed successfully. If it does complete successfully, the callback you write should invoke registrationSucceededForUser, which will presumably update your view.
|

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.