简体   繁体   中英

Swift 4 @escaping type as parameter

I have the following function which makes a GET request to the server. The problem is that I need to provide a specific struct of type Codable as a type for @escaping . Then I use the same type for the JSONDecoder to decode the data received from JSON to Video type.

How Can I provide a type as a parameter to this function. I want to provide for. ex a USER type or CAR type

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

//Another File

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

    }
   }

// Here I call my function "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!)
    }
})

You should be using generics. Just replace Video with a generic parameter what conforms to Codable protocol. And that should be it.

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

Usage

Declare type after the first parameter.

makeGetCall(todoEndpoint: "/path/to/resource") { (video: Video?, error) in

}

Your usage

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

you can create model class for any object you want from this json:

class User {
    let userId: String?
    let userName: String?

    init(jsondata: JSON?) {
        self.userId = jsondata?["user_id"].stringValue
        self.userName = jsondata?["user_name"].stringValue  
    }
}

then create object of this model:

func makeGetCall(todoEndpoint: String, completionHandler: @escaping (User?, Error?) -> Void) {

    // code
    let decoder = JSONDecoder()

    do {
        let todo = User(responsedata)
    } catch {
        print("error trying to convert data to JSON")
        print(error)
        completionHandler(nil, error)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM