func Get <t:evobject>(){}

时间:2018-07-10 08:07:32

标签: ios swift class get

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
        }
    })   
}

我具有用于请求服务器的这种功能,但是对于具有返回功能的GET方法,我该怎么办? 我的Response有一些对象+ GlobalModel,例如我的班级

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 = ""
}

这意味着类联系人具有GlobalModel类的变量,但是我想在我的函数中(如_Delete函数)访问的变量 我的应该是什么?我无法将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
}

1 个答案:

答案 0 :(得分:0)

已经有点晚了,但是我只是打断了你的问题。

由于netwerk调用是异步的,因此您不应尝试从该函数返回数据,因为这可能会花费一些时间。相反,您应该将数据发送回回调中。然后您的功能将变成这样:

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
}

提示:您还可以使用EVReflection的Alamofire扩展来简化对象解析。该项目有一个Alamofire单元测试文件夹,其中包含一些示例,其中也包括类似的泛型。另请参见https://github.com/evermeer/EVReflection/blob/master/UnitTests/AlamofireTests/NestedGenericsIssue25.swift