I am using AlamoFire for my API calls, and have something like this all over my project:
static func login(userName: String, password: String) -> User {
you can use a BaseObject to do this easy :
class BaseUser: ResponseObjectSerializable {
var valueHandle : ((BaseUser)->())?
var errorHandle : ((NSError)->())?
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
}
}
func sendRequest<T:BaseUser>(method: Alamofire.Method, urlString: String, parameters: [String: AnyObject]?) -> T {
let res : T
Alamofire.request(method, urlString, parameters:parameters)
.validate()
.responseObject { (response: Response<T, NSError>) in
switch response.result {
case .Success(let value):
res.valueHandle?(value)
case .Failure(let error):
res.errorHandle?(error)
}
}
return res
}
class
can hold the handle ,and with the baseobj you don't need to write them again and again by using protocol