how can I call CompletionHandler method?

The name of the picture


how can I call CompletionHandler method?



I have already created a method but do not know how to pass parameter and manage response parameter.


class func postWithURL(serverlink:String, methodname:String, param:NSDictionary, key:String, CompletionHandler : @escaping (Bool,NSDictionary) -> ())



thanks





What you are trying to achieve ?
– Prashant Tukadiya
5 hours ago





I have constant File for web service_alomafire. I have write method for request. but I do not know how to call this method from my login controller.
– Bambhroliya Bhavdip
5 hours ago





If you start type method name xcode autocomplete will suggest you a best way to do it. even you don't need to type that; I suggest you to read docs from apple
– Prashant Tukadiya
5 hours ago







see the answer of Sharad Chauhan but don't use NSDictionary in swift 4. Use Dictionary instead
– Prashant Tukadiya
5 hours ago






2 Answers
2



Here is how you can call your method:


self.postWithURL(serverlink: "link", methodname: "name", param:NSDictionary(), key: "key") { (bool, dictionary) in
// you code goes here
}





solved. Thank you.
– Bambhroliya Bhavdip
5 hours ago





can u write code for how to parse response? suppose I want value from reponse than how can I get. please write sample code.
– Bambhroliya Bhavdip
5 hours ago





The response depends upon how you are getting the data from the server. Also what are you using at your networking layer in your app? I would recommend you to look into Alamofire. Its a very handy swift library to make network requests and handle responses. You can find plenty of examples and help for it.
– HAK
3 hours ago



Example of completion method :


func sampleCompletionMethod(name: String, completion:@escaping(Bool)->()) {
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
completion(true)
}
}



This how you call it :


sampleCompletionMethod(name: "Abc") { (result) in
print("Result of completion : (result)")
}



You method should be without class keyword like this :


func postWithURL(serverlink:String, methodname:String, param:NSDictionary, key:String, CompletionHandler : @escaping (Bool,NSDictionary) -> ()) {

}



If your function is in some another class, then make an object of that class, for example if class name is NetworkHelper then add NetworkHelper() to call it.


NetworkHelper


NetworkHelper()



Call it like this :


postWithURL(serverlink: "link", methodname: "POST", param: NSDictionary(), key: "key") { (boolResutl, dictionay) in

}



Edit: Suggested by Prashant Tukadiya



using NSDictionary is bad practice in swift 4. You should use Swift's Native class of Dictionary


NSDictionary


Dictionary





using NSDictionary is bad practice in swift 4. You should use Swift's Native class of Dictionary
– Prashant Tukadiya
5 hours ago


NSDictionary


Dictionary





@PrashantTukadiya Big thank you.
– Bambhroliya Bhavdip
5 hours ago







@BambhroliyaBhavdip You are most welcome :) Keep learning
– Prashant Tukadiya
5 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP