我需要询问有关for循环的问题。我对 for 循环有基本的了解。 我正在尽力理解它。 示例如下:
如何访问/添加该self.imageList到self.photo数组? 我尝试过 self.photo.image = self.imageList[0] 等等。
它看起来不太好,如果缺少照片,我会收到索引错误。 我还尝试将另一个 for 循环放入 for pic in parseProfilePic { 中,该循环以某种方式重复 20 次并最终使用最后一个循环图像。 :( 我想最终 self.photo.image = object["photo"] self.photo2.image = object["photo2"] 等等... 谢谢
var parseProfilePic = [object["photo"], object["photo2"], object["photo3"], object["photo4"]]
var photos = [self.photo, self.photo2, self.photo3, self.photo4]
for pic in parseProfilePic {
pic?.getDataInBackgroundWithBlock({ (imageFiles, error) in
if error == nil {
if let image = UIImage(data: imageFiles!) {
self.imageList.append(image)
}
}
})
}
最佳答案
因此,如果您需要做的就是将 imageList 中的图像(假设这是一个固定长度的数组)附加到您的 socket 上,那么一个简单的开关就可以了。
或者,您可以使用enumerate来获取for中当前项目的索引i,并使用它附加到适当的图像。大致如下:
for (i, item) in imageList.enumerate() {
self.photos[i].image = item
}
干杯。
关于快速理解 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37848613/