如何使@escaping闭包可选?

时间:2017-10-29 22:17:37

标签: swift closures

我在使这个函数通过可选值时遇到困难,我正在使用@escaping闭包,但问题出现在我的代码需要某个参数的地方。所以在这个例子中,我试图上传两个图像。但是,第三个参数给了我麻烦。我怎样才能使某个参数是可选的/不需要调用。如果确实存在,我可以将数据注入某个参数吗?

这是我的代码 -

static func uploadImagesToFirebaseStorage(data: Data? = nil, secondData: Data? = nil, thirdData: Data? = nil, onSuccess: @escaping (_ imageURL: String, _ secondImageURL: String?, _ thirdImageURL: String?) -> Void) {
    let firstPhotoIdString = NSUUID().uuidString
    let secondPhotoIdString = NSUUID().uuidString
    let thirdPhotoIdString = NSUUID().uuidString

    let storageRef = Storage.storage().reference(forURL: Config.STORAGE_REF_ROOT ).child("posts").child(firstPhotoIdString)

    storageRef.putData(data!, metadata: nil) { (metadata, error) in
        if error != nil {
            ProgressHUD.showError(error?.localizedDescription)
            return
        }

        let secondStorageRef = Storage.storage().reference(forURL: Config.STORAGE_REF_ROOT ).child("posts").child(secondPhotoIdString)

        secondStorageRef.putData(secondData!, metadata: nil) { (secondMetadata, error) in
            if error != nil {
                ProgressHUD.showError(error?.localizedDescription)
                secondStorageRef.setValue(nil, forKey: secondPhotoIdString)
                return
            }

            let thirdStorageRef = Storage.storage().reference(forURL: Config.STORAGE_REF_ROOT ).child("posts").child(thirdPhotoIdString)
            thirdStorageRef.putData(thirdData!, metadata: nil) { (thirdMetadata, error) in
                if error != nil {
                    ProgressHUD.showError(error?.localizedDescription)
                    return
                }

                if let firstPhotoURL = metadata?.downloadURL()?.absoluteString, let secondPhotoURL = secondMetadata?.downloadURL()?.absoluteString, let thirdPhotoURL = thirdMetadata?.downloadURL()?.absoluteString {
                    onSuccess(firstPhotoURL, secondPhotoURL, thirdPhotoURL)
                }
            }
        }
    }
}

我会做条件检查,看看是否有有效数据可以通过,但我想要这种结果:

  • 如果值不存在,则在完成条件检查后,我不需要包含某个参数

    • 如果它不存在,请忘记该参数并继续

我发现如果可以创建相同代码的多个实例并更改参数,但我需要20个实例,正如您所知,这样做效率不高。那么如何在不需要/需要每个参数的代码的情况下使所有参数都可选并传递数据呢?

我知道有一个类似的问题已被问到,但我读了它并没有发现它有用。

谢谢。

编辑 - 更多信息

我正在尝试将图片上传到Firebase。我有三个参数(对于3个图像),但是,我希望该功能可以扩展,因为它没有多少图像,我可以相应地上传。

这是我对函数的调用:

static func uploadDataToServer(data: Data, secondData: Data? = nil, thirdData: Data? = nil, firstVideoURL: URL? = nil, secondVideoURL: URL? = nil, thirdVideoURL: URL? = nil, caption: String, onSuccess: @escaping () -> Void) {

        if let secondImageData = secondData {
            uploadImagesToFirebaseStorage(data: data, secondData: secondImageData) { (firstPhotoURL, secondPhotoURL, nil )  in
                self.sendDataToDatabase(firstPhotoURL: firstPhotoURL, secondPhotoURL: secondPhotoURL, caption: caption, onSuccess: onSuccess)
            }
        }

    }
}

在上面的代码中,我试图上传两个图像,甚至认为我有三个参数因此为什么我将除了第一个数据AKA第一个图像之外的所有图像设置为零。我的问题是代码要么想要第三个图像,要么因为没有第三个图像而崩溃。我通过传递图像(来自ImagePickerController)并使用UIImageJPEGRepresentation()从ViewController类中注入图像。

0 个答案:

没有答案
相关问题