I know this type of questions are asked before but i am working on to achieve the following structure in swift app.
Please consider this as example. I want to show top25 apps of Appstore in table-view
For http request i am using Alamofire.
AppManager.swift -> This class file will be responsible for Making webservice call using Alamofire Methods which are written in ApiServiceHelper.swift.
App.swift -> Model class which will parse the json and create the model
ApiServiceHelper.swift -> A helper class wrapper, where i can define the fucntions and here i will Put the Alamofire methods which will communicate with back-end and get response.
I have created a App.swift and AppManager.swift class. Here is my ApiServiceHelper.swift code :
import Alamofire
import SwiftyJSON
public class ApiService : NSObject {
typealias ApiServiceRequestSuccess = (success : Bool? , response : NSDictionary? , serverError : NSError?) -> (Bool?,NSDictionary?,NSError?)
typealias ApiServiceRequestFail = (error : NSError?) -> Void
func requestWithparams (params : NSDictionary, type : App.ApiManagerRequestType, apiName : String, successBlock :ApiServiceRequestSuccess , errorBlock : ApiServiceRequestFail) {
let webserviceUrl = "\(App.url)\(apiName)"
Manager.sharedInstance.request(.POST, webserviceUrl, parameters: params as? [String : AnyObject], encoding: ParameterEncoding.URL, headers: ["Content-Type" : "application/json"]).validate().responseJSON { (Response) -> Void in
if Response.result.isSuccess {
successBlock(success: <#T##Bool?#>, response: <#T##NSDictionary?#>, serverError: <#T##NSError?#>)
} else {
errorBlock(error: <#T##NSError?#>))
}
}
}
}
I have created this type of helper class for AFNetworking which works really well in Objective-c.
When i am returning the closures, It gives the error. I am not able to get the NSDictionary in AppManager.swift which is returned by ApiServiceHelper.swift
Is above approach is right to handle request and response in Swift APP ? How can i create this helper class more proper with closures ?
Any guidance to Achieve above structure and comments about above structure ?
I am able to parse the code if I directly use the alamofire request methods. But What i am trying to achive is clean code separation and More code re-usability.
Any help, or a code for this helper file is helpful. Thanks in advance.