Alamofire 的通用功能

Generic function with Alamofire

我与使用 Alamofire 的 iOS 应用程序一起工作,我想编写一个通用函数,用于从服务器向可解码对象发送和检索数据,我的函数是下面:

func pop <T : Codable>  (_ Url: inout String, _ popedList: inout [T]) {
    let url = URL(string:Url)
    Alamofire.request(url!, method: .post).responseJSON { response in
        let result = response.data
        do {
            let data = try JSONDecoder().decode(popedList, from: result!)// get error here
            print(data[0])

            let jsonEncoder = JSONEncoder()
            let jsonData = try! jsonEncoder.encode(data[0])
            let jsonString = String(data: jsonData, encoding: .utf8)
            print("jsonString: \(String(describing: jsonString))")

        } catch let e as NSError {
            print("error : \(e)")
        }
    }
} 

和一个将对象发送到服务器的函数,如下所示:

func push <T : Codable>  (_ Url: inout String, _ pushObject: inout T) {
    let jsonData = try! JSONEncoder().encode(pushObject)
    let jsonString = String(data: jsonData, encoding: .utf8)
    print("jsonString: \(String(describing: jsonString))")

    let url = URL(string:Url)

    Alamofire.request(url!,
                      method: .post,
                      parameters:jsonString)//it's need to creat a Dictionary instate of String
        .validate(statusCode: 200..<300)
        .validate(contentType: ["application/json"])
        .response { response in
            // response handling code
             let result = response.data
            print(response.data)
    }
}

我在第一个函数中遇到错误,

"Cannot invoke 'decode' with an argument list of type '([T], from: Data)'"

"Escaping closures can only capture inout parameters explicitly by value"

将它们编写为通用函数的最佳方法是什么?

JSONDecoder().decode 方法采用类型和数据参数。通行证类型不是popedList.

let data = try JSONDecoder().decode([T].self, from: result!)

输入输出参数 默认情况下,函数参数是常量。尝试从函数体内更改函数参数的值会导致编译时错误。这意味着您不能错误地更改参数的值。如果您希望函数修改参数的值,并且希望这些更改在函数调用结束后仍然存在,请将该参数定义为输入输出参数。

您没有在两个函数中更改 popedList 的值,因此使用 inout 没有意义。

对于第一个函数,JSONDecoder.decode() 需要 2 个参数:

  • 要解码的类型:class/struct 您希望解码的类型。这不是实例化对象,只是类型。
  • 要解码的数据:将转换为您指定类型的通用数据对象。

因此,为了能够编写您的函数,使其具有通用 URL 和结果对象,您需要将对象类型传递给它,并需要将结果传递给回调,因为网络操作是异步的。

func dec<T: Decodable>(from: URL, decodable: T.Type, result: (T) -> Void) { 
    // your Alamofire logic
    let data = try JSONDecoder().decode(popedList, from: result!)
    result(data)
}

您可以将相同的逻辑应用于第二个函数。

请注意,这不是处理最终错误的最佳方法,只是一个示例,说明如何使用通用函数处理编码。

经过几次搜索并尝试编辑我的函数后,我能够重写我的两个函数,以便我得到我需要的东西:

 func pop<T: Decodable>(from: URL, decodable: T.Type, completion:@escaping (_ details: [T]) -> Void)
            {
       Alamofire.request(from, method: .post).responseJSON { response in
                let result_ = response.data
                do {
                    let data = try JSONDecoder().decode([T].self, from: result_!)
                    //let data = try JSONDecoder().decode(decodable, from: result_!)// get error here
                    //print(data[0])
                    print("data[0] : \(data[0])")
                    completion(data)
                } catch let e as NSError {
                    print("error : \(e)")
                }
            }
        }

    func push <T : Codable>  (_ Url:  String, _ pushObject:  T)
        {
            let jsonData = try! JSONEncoder().encode(pushObject)
            let jsonString = String(data: jsonData, encoding: .utf8)
            print("jsonString: \(String(describing: jsonString))")

            let url = URL(string:Url)

            Alamofire.request(url!,
                              method: .post,
                              parameters:convertToDictionary(text: jsonString!))//it's need to creat a Dictionary instate of String
                .validate(statusCode: 200..<300)
                .validate(contentType: ["application/json"])
                .response { response in
                    // response handling code
                    print(response.data!)
                    if let jsonData = response.data {
                        let jsonString = String(data: jsonData, encoding: .utf8)
                        print("response.data: \(String(describing: jsonString))")
                    }
            }
        }

        func convertToDictionary(text: String) -> [String: Any]? {
            if let data = text.data(using: .utf8) {
                do {
                    return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                } catch {
                    print(error.localizedDescription)
                }
            }
            return nil
        }