简体   繁体   中英

Completion methods in custom framework

My main program is calling a method in my custom framework and I want it to call a method in my main program when complete. What is the best method to do this in Swift?

The method I am calling is a service, so I can't expect it to return quickly.

This is in my framework:

public func login(server: String, password: String, username: String, url: String, completion : (success: Bool, json: AnyObject?) -> Void) -> UserProfile {

    completion(success: true, json: json)

    return profile
}

This is how I cam calling it from my main program and I think that I am doing it wrong

services.login(hostToUse, password:self.passwordField.text!, username: self.emailField.text!, url: "http://\(hostToUse)/user/login", completion: testCompletion())

You're calling it wrong. call like so:

 services.login(hostToUse, password:self.passwordField.text!, username: self.emailField.text!, url: "http://\(hostToUse)/user/login") {(success, jsonData) in

    if success {
        // Your success code here
    }
    else {
        // Your failure code here
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM