我一直在尝试让这段代码在过去几个小时内运行。但是我的结果好坏参半。
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 firstVideoURL = firstVideoURL {
SingleVideoHandler.uploadOneVideoToFirebase(firstVideoURL: firstVideoURL, onSuccess: { (vidURL) in
uploadImagesToFirebaseStorage(data: data, secondData: secondData!, thirdData: thirdData!, onSuccess: { (firstImage, secondImage, thirdImage) in
sendDataToDatabase(firstPhotoURL: firstImage, secondPhotoURL: secondImage, thirdPhotoURL: thirdImage ,firstVideoURL: vidURL, caption: caption, onSuccess: onSuccess)
})
})
} else {
if let firstVideoURL = firstVideoURL, let secondVideoURL = secondVideoURL {
print("Got two pics!")
MultipleVideoHandler.uploadTwoVideosToFirebase(firstVideoURL: firstVideoURL, secondVideoURL: secondVideoURL, onSuccess: { (firstVidURL, secondVidURL) in
uploadImagesToFirebaseStorage(data: data, secondData: secondData!, thirdData: thirdData!, onSuccess: { (firstImage, secondImage, thirdImage) in
sendDataToDatabase(firstPhotoURL: firstImage, secondPhotoURL: secondImage, thirdPhotoURL: thirdImage, firstVideoURL: firstVidURL, secondVideoURL: secondVidURL, caption: caption, onSuccess: onSuccess)
})
})
} else {
if let secondImageData = secondData, let thirdImageData = thirdData {
uploadThreeImagesToFirebaseStorage(data: data, secondData: secondImageData, thirdData: thirdImageData) { (firstPhotoURL, secondPhotoURL, thirdPhotoURL) in
self.sendDataToDatabase(firstPhotoURL: firstPhotoURL, secondPhotoURL: secondPhotoURL, thirdPhotoURL: thirdPhotoURL, caption: caption, onSuccess: onSuccess)
}
}
}
}
}
基本上,我正在尝试检测我有firstVideoURL
,secondVideoURL
和thirdVideoURL
。
但是,要获得secondVideoURL
,您必须拥有firstVideoURL
。关于拥有thirdVideoURL
的问题,您必须同时拥有firstVideoURL
和secondVideoURL
。
我在做什么,实质上它允许可选视频,用户可以选择最多三个视频上传和图像。由于测试,目前只有3个参数。
我正在尝试检测我拥有多少个videoURL,并且基于此,上传但是已经选择了许多视频和其他内容。
我的结果到目前为止:
仅调用并执行第一个if-let
语句。甚至以为我有多个videoURL。
代码被多次调用,创建相同的帖子实例,但是,第一个实例将只包含firstVideoURL,第二个实例将同时包含第一个和第二个视频URL。第二个例子就是我想要的。但我不想要第一个例子。
我无法弄清楚为什么会这样?
有人可以帮帮我吗?
谢谢。
实质上这就是我想要的 -
我有2个视频网址
代码被执行
检查条件
跳过第一个if-let statement
执行第二个if-let statement
。
在上传和创建帖子方面,server / api代码按预期工作。只是这个条件操作没有像我预期的那样工作。
答案 0 :(得分:1)
如果存在firstVideoURL,则第一个if条件(如果让firstVideoURL = firstVideoURL)通过,则它不关心secondVideoURL和/或thirdVideoURL。如果这就是你需要的,你应该考虑构建你的if-else,如下所示:
if let firstVideoURL = firstVideoURL{
if let secondVideoURL = secondVideoURL{
if let thirdVideoURL = thirdVideoURL{
//Do what you would if there are all the three video urls.
} else {
//Do what you would if there are firstVideoURL and secondVideoURL but not the thirdVideoURL
}
} else {
//Do what you would if there is only the firstVideoURL
}
} else {
//Handle the case when there are no video urls.
}
答案 1 :(得分:1)
您是否允许在没有视频网址的情况下调用它?如果是这样,为什么?您已将所有3个videoURL参数设为可选,这似乎没有意义。在我看来,你应该允许一个,两个或三个。
在任何情况下,您的代码都没有意义。一旦第一个if子句失败,你知道firstVideoURL是nil,所以第二个if子句也将失败。
如果您继续采用这种方法,我认为@CodeDifferent对他们的评论有正确的想法:
if let firstVideoURL = firstVideoURL,
let secondVideoURL = secondVideoURL,
let thirdVideoURL = thirdVideoURL {
//upload 3 videos
} else if let firstVideoURL = firstVideoURL,
let secondVideoURL = secondVideoURL {
//upload 2 videos
} else if let firstVideoURL = firstVideoURL {
//upload 1 video
} else {
//No videos. Why is this possible?
}
(我真的不明白你对视频网址和数据对象的使用,所以我忽略了那一部分。)
但是,让我们退后一步。为什么使用3组独立的可选参数并处理if let
可选绑定的混乱构造?为什么不传入一系列视频网址?
func uploadDataToServer( data: [Data], urls: [URL]) {
//Loop through the array of data objects/URLs and upload each one.
}