Swift 如何访问嵌套循环中的变量?
我试图访问嵌套循环中的“x”值,但每次尝试时,内部循环中的“x”值都会发生变化Swift 如何访问嵌套循环中的变量?,swift,for-loop,cocoa-touch,Swift,For Loop,Cocoa Touch,我试图访问嵌套循环中的“x”值,但每次尝试时,内部循环中的“x”值都会发生变化 var downloads_array = [[URL]]()//edited func downloadImages(){ var count = images.count let storage = Storage.storage().reference() for x in 0...count-1{ var anime = [Strin
var downloads_array = [[URL]]()//edited
func downloadImages(){
var count = images.count
let storage = Storage.storage().reference()
for x in 0...count-1{
var anime = [String]()
anime.append(images[x])
anime.append(images[x] + "1")
print("outside the second loop x is : \(x)")
for i in anime{
let storageRef = storage.child("images/\(i).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
print(x)
self.downloads_array[x].append(url!)//edited
}
}
}
}
}
输出为:
outside the second loop x is : 0
outside the second loop x is : 1
outside the second loop x is : 2
outside the second loop x is : 3
outside the second loop x is : 4
outside the second loop x is : 5
0
3
0
1
1
4
2
5
3
4
2
5
我是swift development的新手,请指导我解决此问题。您的
storageRef.downloadURL
是一个异步调用,这意味着执行完成块需要时间。将执行for
循环,并且将命中count
*2个调用。根据收到每个响应的速度,将调用相应的完成操作,从而获得所需的顺序
func downloadImages(){
var count = images.count
let storage = Storage.storage().reference()
for x in 0...count-1{
var anime = [String]()
anime.append(images[x])
anime.append(images[x] + "1")
print("outside the second loop x is : \(x)")
print(x) // printing it here will give you the order as is
for i in anime {
let storageRef = storage.child("images/\(i).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
print(x)
}
}
}
}
}
因为您无法控制完成块的执行顺序。字典是解决你问题的一种方法。将索引存储为键,将url数组存储为值。或者您可以有一个结构,它存储相应索引的URL并对其进行排序。您的
storageRef.downloadURL
是一个异步调用,这意味着执行完成块需要时间。将执行for
循环,并且将命中count
*2个调用。根据收到每个响应的速度,将调用相应的完成操作,从而获得所需的顺序
func downloadImages(){
var count = images.count
let storage = Storage.storage().reference()
for x in 0...count-1{
var anime = [String]()
anime.append(images[x])
anime.append(images[x] + "1")
print("outside the second loop x is : \(x)")
print(x) // printing it here will give you the order as is
for i in anime {
let storageRef = storage.child("images/\(i).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
print(x)
}
}
}
}
}
因为您无法控制完成块的执行顺序。字典是解决你问题的一种方法。将索引存储为键,将url数组存储为值。或者您可以有一个结构,它存储相应索引的URL,并对其进行排序。不确定您到底需要什么,但我认为您正在尝试实现这样的功能:-
func downloadImages(){
var count = images.count
let storage = Storage.storage().reference()
var data : [String:Int] = []
for x in 0...count-1{
var anime = [String]()
anime.append(images[x])
anime.append(images[x] + "1")
print("outside the second loop x is : \(x)")
for i in anime {
let storageRef = storage.child("images/\(i).jpg")
data["YourUrl"] = x // Replace YourUrl with your url string
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
if let value = data[url.absoluteString]{
print(value)
}
}
}
}
}
}
不确定您到底需要什么,但我认为您正在尝试实现以下内容:-
func downloadImages(){
var count = images.count
let storage = Storage.storage().reference()
var data : [String:Int] = []
for x in 0...count-1{
var anime = [String]()
anime.append(images[x])
anime.append(images[x] + "1")
print("outside the second loop x is : \(x)")
for i in anime {
let storageRef = storage.child("images/\(i).jpg")
data["YourUrl"] = x // Replace YourUrl with your url string
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
if let value = data[url.absoluteString]{
print(value)
}
}
}
}
}
}
storageRef.downloadURL
是async
,因此代码将继续执行,并且x
值将更改。这里的解决方案是通过中间变量或块捕获来捕获当前的x值
块捕获
-
中间变量
storageRef.downloadURL
是async
,因此代码将继续执行,并且x
值将更改。这里的解决方案是通过中间变量或块捕获来捕获当前的x值
块捕获
-
中间变量
由于Firebase存储是一个异步调用(网络调用),我们无法保留该值,因为顺序将是非顺序的。因此,我创建了一个带有字符串和URL的字典来存储图像的相应下载URL,因为Firebase存储是一个异步调用(网络调用),我们不能保留该值,因为顺序是非顺序的。因此,我创建了一个带有字符串和URL的字典来存储图像的相应下载URLHi Sathis Kumar,你能指定你要查找代码的响应/结果是什么吗,我想根据xf的值附加循环的下载url,以获得正确答案,您需要向我提供示例数据,如下载数组值、图像值以及您要查找的输出。您好Sathis Kumar,请指定您要查找代码的响应/结果。您好,兄弟,我有一个类型为[[URL]]()的数组,我想根据XF的值附加循环的下载URL,以获得正确的答案,您需要向我提供示例数据,如downloads_数组值、images值以及您要查找的输出。如何使其同步bro?你能给我解释一下或者指导我如何学习同步和同步吗asynchronous@sathishkumar网络调用本质上是异步的。如果你想让它们同步,你应该在收到前一个网络呼叫后才拨打网络呼叫。然后,您需要重新构造代码,以便从完成时开始命中网络调用。在您要求我打印“x”的地方,我创建了一个变量,并将其赋值为let y=x,但值仍然是随机的(如00,11,32,23,44,55)。我希望这些数字将url值附加到数组中。怎么做?@sathishkumar那是我的错。编辑过。@sathishkumar我能想到的一个可能的解决方案是使用字典存储url,索引作为键,url数组作为值。如何使其同步?你能给我解释一下或者指导我如何学习同步和同步吗asynchronous@sathishkumar网络调用本质上是异步的。如果你想让它们同步,你应该在收到前一个网络呼叫后才拨打网络呼叫。然后,您需要重新构造代码,以便从完成时开始命中网络调用。在您要求我打印“x”的地方,我创建了一个变量,并将其赋值为let y=x,但值仍然是随机的(如00,11,32,23,44,55)。我希望这些数字将url值附加到数组中。怎么做?@sathishkumar那是我的错。编辑过。@sathishkumar我能想到的一个可能的解决方案是使用字典存储url,索引作为键,url数组作为值。你好,兄弟,谢谢你的努力。我的逻辑是有一个名为downloaded_images=[[URL]]()的数组,当有一个名为(i)的图像出现时,我想像[[URL_image1,URL_image2],…]那样附加该URL。为此,我需要“x”的整数值。你试过我的代码吗?只需将数据[“YourUrl”]替换为您的url地址,并检查循环末尾的数据值,您将使用url及其索引作为值尝试@sathishkumard,我不知道在您的情况下url是url或字符串,因此您必须注意将url字符串添加到数据目录中所需的事项