I am new to swift. And I am working on swift project. I have getting response from server response like follows
kNetworkManager.executePostRequest(urlString: kAppSocialLoginURL, params:mainDictionary, requestVC: self,completionHandler: {(responseObject) -> () in
// print("response object:\(responseObject!)")
if responseObject != nil {
let responseDictionary = responseObject as! NSDictionary
if responseDictionary is NSDictionary{
let obj = responseDictionary.value(forKey:"user")
if obj is NSDictionary{ //success case
print("NSDictionary is",obj ?? NSDictionary())
UtilityClass.sharedInstance.userDetailsDictionary = responseDictionary as! [String : AnyObject]
if let obj = responseDictionary.value(forKey:"user") as? NSDictionary {
if let sessionId = obj["token"] as? String {
UtilityClass.sharedInstance.kSessionIDString = sessionId
}
if let userObj = obj["user"] as? NSDictionary {
In above code, I am checking multiple times whether its dictionary or not, or assign as dictionary. I am doing this kind exercise in all my classes. So, I want to create some modal class for common logic, And I want to use that logic every class after getting Webservice data.
And my response from webservice is follows
user = {
sessid = "-qadadadad";
"session_name" =aadadad;
tokenData = adadadad;
user = {
access = 1513647;
created = 14822;
data = {
"ckeditor_auto_lang" = t;
"ckeditor_default" = t;
"ckeditor_lang" = en;
"ckeditor_show_toggle" = t;
"ckeditor_width" = "100%";
contact = 1;
};
And I am using Alamofire for API calls.
Can anyone help me here, to achieve this? Thanks!
If you using Alamofire then. Use this Common method
Please adjust this Methods according to you because i am directly copy this methods in my network common class
Since you are using Alamofire in your
kNetworkManager
, I would suggest to also use AlamofireObjectMapper to get rid of the hassle of parsing the response and mapping it into a model:It is easy to be integrated, you could check its documentation to get familiar of how you could do it.
Based on the response you mentioned, the mappable object should similar to:
Also, if you are using Swift 4, you might want to take a look at
Codable
, it should make your life easier!