Rex*_*xha 2 xcode ios swift swift4
我有以下功能GET向服务器发出请求.问题是我需要提供特定struct类型Codable作为类型@escaping.然后我使用相同类型的JSONDecoder来解码从JSON接收的数据到Video类型.
如何将类型作为参数提供给此函数.我想提供.一种USER类型或CAR类型
struct Video: Codable {
var title: String
var pretty_artists: String
var yt_id: String
var views: String
var video_name: String
var published: Published
var result: Bool
init(title: String = "", pretty_artists: String = "", yt_id: String = "", views: String = "", video_name: String = "", published: String = "", result: Bool = true) {
self.title = title
self.pretty_artists = pretty_artists
self.yt_id = yt_id
self.views = views
self.video_name = video_name
self.published = Published()
self.result = result
}
}
Run Code Online (Sandbox Code Playgroud)
//另一个文件
class XHR {
// Video to be Dynamic
func makeGetCall(todoEndpoint: String, completionHandler: @escaping (Video?, Error?) -> Void) {
// code
let decoder = JSONDecoder()
do {
let todo = try decoder.decode(Video.self, from: responseData)
completionHandler(todo, nil)
} catch {
print("error trying to convert data to JSON")
print(error)
completionHandler(nil, error)
}
}
}
Run Code Online (Sandbox Code Playgroud)
//这里我调用我的函数"ViewController.swift"
// Initial request
xhr.makeGetCall<XHR>(todoEndpoint: "https://kida.al/search/uh/onajr", { result, err in
if(result != nil) {
self.ytPlayer.load(withVideoId: result!.yt_id, playerVars: self.playerVars)
self.updateVideo(data: result!)
}
})
Run Code Online (Sandbox Code Playgroud)
你应该使用泛型.只需用Video符合Codable协议的通用参数替换即可.这应该是它.
func makeGetCall<T>(todoEndpoint: String, completionHandler: @escaping (T?, Error?) -> Void) where T: Codable {
// As step one, you need to do networking to fetch `responseData`
// code
let decoder = JSONDecoder()
do {
let todo = try decoder.decode(T.self, from: responseData)
completionHandler(todo, nil)
} catch {
print("error trying to convert data to JSON")
print(error)
completionHandler(nil, error)
}
}
Run Code Online (Sandbox Code Playgroud)
用法
在第一个参数后声明类型.
makeGetCall(todoEndpoint: "/path/to/resource") { (video: Video?, error) in
}
Run Code Online (Sandbox Code Playgroud)
你的用法
class XHR {
enum Result<T> {
case success(T)
case failure(Error)
}
func makeGetCall<T>(todoEndpoint: String, completionHandler: @escaping (Result<T>) -> Void) where T: Codable {
// code
let decoder = JSONDecoder()
do {
let todo = try decoder.decode(T.self, from: responseData)
completionHandler(.success(todo))
} catch {
print("error trying to convert data to JSON")
print(error)
completionHandler(.failure(error))
}
}
}
// Initial request
let xhr = XHR()
xhr.makeGetCall(todoEndpoint: "https://kida.al/search/uh/onajr") { (result: XHR.Result<Video>) in
switch result {
case .failure(let error):
// Ups, there is something wrong
print(error)
case .success(let video):
// Sal goodman
self.ytPlayer.load(withVideoId: video.yt_id, playerVars: self.playerVars)
self.updateVideo(data: video)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
997 次 |
| 最近记录: |