Search code examples
iosswiftclassget

func Get<T:EVObject>(){}


public func _Delete(contentType:String,url:String,callback:@escaping (Bool,String)->Void){
    let headers :Dictionary<String,String> = ["Content-Type":contentType]
    Alamofire.request(url,method: .delete,headers: headers).validate(statusCode: 200..<300).responseJSON(completionHandler: {response in
        let resp = GlobalModel(json:String(data:response.data!,encoding:.utf8))
        switch resp.status {
        case 1:
            callback(true,"")
        case -2:
            callback(false,resp.message)
        case -1:
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.loginExpired()
        default:
            break
        }
    })   
}

i have this kind of func for requesting to server but for GET method that has return what should i do? my Response has some objects + GlobalModel for example my class is

class contact:EVOBject {
public var phone:Int=0
public var mobile:Int = 0
public var status:Int = 0
public var message:String = ""
}
class GlobalModel:EVObject{
public var status:Int = 0 
public var message:String = ""
}

that means class contact have variable of class GlobalModel but i want to access that variable of < T > in my function like _Delete function what my < T > should be? i cant declare T as EVObject and GlobalModel like < T:EVObject,GlobalModel >

public func _Get<T:EVObject>(contentType:String,url:String,callback:@escaping (Bool,String)->Void)-> [T]{
    let headers : Dictionary<String, String> = ["Content-Type":contentType];
    var result:[T] = []
    Alamofire.request(url,method: .get,encoding: JSONEncoding.default,headers: headers).validate(statusCode: 200..<300).responseJSON(completionHandler: {response in
        switch response.result{
        case .success:
            if let data = response.data, let utf8Text = String(data:data,encoding:.utf8){
                result = [T](json:utf8Text)
            }
            callback(true,"")
        case .failure:
            if(response.response?.statusCode == 401) {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.loginExpired()
            }
            callback(false,"")
        }
    })
    return result
}

Solution

  • It's a little late, but I just stubbed on your question.

    Since the netwerk call is async you should not try to return the data from the function because that could potentially take quite some time. Instead you should send the data back in the callback. Your function would then become something like this:

    public func _Get<T:EVObject>(contentType:String,url:String,callback:@escaping (Bool,[T]) -> Void) -> Void{
        let headers : Dictionary<String, String> = ["Content-Type":contentType];
        var result:[T] = []
        Alamofire.request(url,method: .get,encoding: JSONEncoding.default,headers: headers).validate(statusCode: 200..<300).responseJSON(completionHandler: {response in
            switch response.result{
            case .success:
                if let data = response.data, let utf8Text = String(data:data,encoding:.utf8){
                    result = [T](json:utf8Text)
                }
                callback(true, result)
            case .failure:
                if(response.response?.statusCode == 401) {
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.loginExpired()
                }
                callback(false, [])
            }
        })
        return result
    }
    

    Tip: You could also use the Alamofire extension of EVReflection for even easier object parsing. The project has an Alamofire unit test folder with some samples that also include generics like this. See also https://github.com/evermeer/EVReflection/blob/master/UnitTests/AlamofireTests/NestedGenericsIssue25.swift