user1079052 user1079052 - 5 months ago 9
Swift Question

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())

Answer

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{

}
else{

}

}