结构作为函数的参数与完成 - Swift

问题描述 投票:-1回答:2

我遇到麻烦,有可能在完成内部设置一个函数参数吗?具体来说,我有2个结构,我希望用户将选择其中一个。我的代码是这样的:

struct Photo: Decodable {
...
}
//Function
func fetchPhotos(url: String, completion: @escaping ([Photo]?, Error?) -> ()) {
...
}

基本上我想要而不是[Photo]?在完成中,我将可以设置参数。这可能吗?

谢谢!

swift parameter-passing func
2个回答
3
投票

创建一个协议,并在它们上确认并传递它作为参数,你不关心对象是什么类型它你只需要知道如何使用它,这就是为什么在完成之后你只需要转换它的类型是你需要的,检查下面的代码,我添加了Video struct作为你的第二个,因为你没有问题。

protocol Switchable: Decodable {

}

struct Video: Switchable {

}
struct Photo: Switchable {

}
//Function
func fetchPhotos(url: String, completion: @escaping ([Switchable]?, Error?) -> ()) {
     completion([],nil)
}
//Check on the result after completion is called
let v: [Switchable] = [Video(), Video()]

if let photos = v as? [Photo] {
    print("its photos", photos)
}
if let videos = v as? [Video] {
    print("its videos ",videos)
}

0
投票

除了Mohmmad S answer,为了实现fetchPhotos,似乎使用枚举更合适,因为在completion参数中我们称为“结果或错误”。

你可以实现类似的东西:

enum CustomError: Error {
    case notFound
}

enum Result<T> {
    case success(T)
    case failure(CustomError)
}

protocol Switchable: Decodable { }
struct Video: Switchable { }
struct Photo: Switchable { }

因此,fetchPhotos为:

func fetchPhotos(url: String, completion: @escaping (Result<[Switchable]>) -> Void) {
    // in case of error:
    let error: CustomError? = CustomError.notFound
    if let error = error {
        completion(.failure(error))
    }

    // in case of success - videos:
    completion(.success([Video(), Video()]))

    // in case of success - photos:
    completion(.success([Photo(), Photo()]))
}

将其称为:

fetchPhotos(url: "...") { result in
    switch result {

    case .success(let photos):
        // in case of photos
        if let photos = photos as? [Photo] { /**/ }

        // in case of videos
        if let videos = photos as? [Video] { /**/ }

        // in case of mix of photos and videos, you should iterate through it and check each object
        photos.forEach({ photo in
            if let media = photo as? Photo {

            } else if let media = photo as? Video {

            }
        })

    case .failure(let error):
        print(error.localizedDescription)

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.