我在Swift中有一些基础知识,现在我正在尝试学习iOS开发。我目前正在一个小型应用程序中工作,该应用程序会询问我所做的API资源,该API返回JSON由以下内容:
struct A : Codable {
let name: String
let age: Int
}
struct B : Codable {
let something: String
}
API和应用都有这些结构定义。由于我一直在查询相同的API,因此我想包装询问API的部分并解码此部分,因此我有一个在回调中使用的结构实例。这是此方法:
static func getContent(urlRequest: URLRequest, decodable: Decodable, completion: @escaping (Codable?, ErrorEnum?)->Void) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest) {
data, response, error in
guard let data = data else {
completion(nil, .noData) // Handling errors in an enum
return
}
let decoder = JSONDecoder()
if let full = try? decoder.decode(decodable, from: data) {
completion(full, nil)
}
}
task.resume()
}
我的问题涉及可解码的参数。这显示了一个错误,并阻止我编译应用程序。在stackoverflow上找到一些资源后,我尝试将参数更改为
static func getContent(urlRequest: URLRequest, decodable: Decodable.Type, completion: @escaping (Codable?, ErrorEnum?)->Void)
我还尝试保留这样的参数,而是在解码参数内更改
if let full = try? decoder.decode(decodable, from: data) {
completion(full, nil)
}
,但似乎没有任何满足编译器...在Swift源代码中查看decode
方法对我没有什么帮助,因为它需要T.Type where T is Decodable
我的愿望是能够使用以下内容:
static func getA() {
guard let url = URL(string: "http://localhost/a") else { return }
let urlRequest = URLRequest(url: url)
getContent(urlRequest: urlRequest, decodable: A.self) {
a, error in
guard a = a else { return }
print(a.name!)
}
}
您是否知道如何实现这一目标?我也不真的知道如何称呼这种类型的参数或在Google上搜索的内容,这可能会导致我找到答案(缺乏词汇)。
谢谢!
尝试一下,只需添加Codable
的generic
.Type
,然后将其类型用作参数通过foo.self
static func getContent<T: Codable>(urlRequest: URLRequest, decodable: T.Type, completion: @escaping (T?, ErrorEnum?)->Void) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest) {
data, response, error in
guard let data = data else {
completion(nil, .noData) // Handling errors in an enum
return
}
let decoder = JSONDecoder()
if let full = try? decoder.decode(decodable, from: data) {
completion(full, nil)
}
}
task.resume()
}
您可以使用以下方式:
func genericRequest<T: Decodable>(_ request: URLRequest, completion: @escaping APIGenericRequestCompletion<T>) {
Alamofire.request(request).responseData { (response) in
guard let data = response.data else {
completion(nil)
return
}
do {
let decodedObject = try JSONDecoder().decode(T.self, from: data)
completion(decodedObject)
} catch {
completion(nil)
}
}
}
其中apigenericrequestcompletion是:
typealias APIGenericRequestCompletion<T: Decodable> = (_ result: T?) -> Void
然后您将其用作:
genericRequest(request) { (decodableObjectResponse) in
// your code here
}