当我尝试遍历以下代码时,self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)函数应具有currentIterationi,该值应等于0,但是运行该代码时,它不等于i。

for (var i = 0; i < self.numberOfImages; i++) {
                    let displayedPicture = object["productImage\(i)"] as? PFFile
                    self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)
                    var oldI = -1
                    print(object.objectId)
                    print("oldI \(oldI)")
                    print("i = \(i)")
                    print("Getting objects")
                    // self.productImages.append((object["productImage\(i)"] as? PFFile)!)
                    print("oldI = i")
                    oldI++
                }


这就是控制台输出的内容


成功
在businessDetailsVC上
图片数量2
对象数可选(1)
可选(“ H1b7iUj4PS”)
我-1
我= 0
获取对象
我=我
可选(“ H1b7iUj4PS”)
我-1
我= 1
获取对象
我=我
currentIteration = 1
图像数量= 2
ImageViewArray计数= 1
致命错误:数组索引超出范围
(lldb)


这是returnImage函数。好像该函数迟到了,因为它从Parse后端检索数据,这很明显,因为prints在函数被调用之前就被调用了,即使它与代码相反。

func printValue(result: Bool) {
    print("result = \(result)")
}

func returnImage(pictureFile: PFFile, let currentIteration: Int, completion: (result: Bool) -> Void) {
    pictureFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
        if error != nil {
            print(error)
            completion(result: false)
        } else {
            if let data = imageData {
                print("currentIteration = \(currentIteration)")
                //var circleArray = Array<UIImageView>()
                //var y : CGFloat = 0.0
                //var i = 0
                self.imageViewArray.append(UIImageView(frame:CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 80), 120, 120)))
                print("Number Of Images Count = \(self.numberOfImages)")
                print("ImageViewArray Count = \(self.imageViewArray.count)")

                self.imageView = self.imageViewArray[currentIteration] // This is the line where the Array index out of range occurs

                self.imageView.image = UIImage(data: data)!
                self.imageView.tag = currentIteration
                self.view.addSubview(self.imageView)

                //self.imageView[i].frame = CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 10), 60, 60)
                //self.imageView[i].image = UIImage(data: data)!
                self.imageView.contentMode = UIViewContentMode.ScaleAspectFill
                self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
                self.imageView.clipsToBounds = true

            }
        }

        completion(result: true)
    })
}


这是完整的代码。 self.imageView = self.imageViewArray[currentIteration]在行上发生数组索引超出范围错误

import UIKit
import Parse

class BusinessDetailsVC: UIViewController, UIScrollViewDelegate {

var businessTitle: String = ""

let scrollView: UIScrollView = UIScrollView()

let screenHeight = UIScreen.mainScreen().bounds.size.height
let screenWidth = UIScreen.mainScreen().bounds.size.width

var numberOfImages: Int = 0

var nameOfImage: [String] = [String]()

var productImages: [PFFile] = [PFFile]()

var downloadedImage: UIImage = UIImage()

var imageViewArray = Array<UIImageView>()
var imageView: UIImageView = UIImageView()

//var displayedPicture: [PFObject] = [PFObject]()
//var imageView = UIImageView()

override func viewDidLoad() {
    super.viewDidLoad()

    print("On businessDetailsVC")

    let query = PFQuery(className: "C_h0usYnpEqL")

    print("Number of images \(numberOfImages)")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if error != nil {
            print(error)
        } else if objects! == objects! {
            print("Number of objects \(objects?.count)")
            for object in objects! {
                for (var i = 0; i < self.numberOfImages; i++) {
                    let displayedPicture = object["productImage\(i)"] as? PFFile
                    self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)
                    var oldI = -1
                    print(object.objectId)
                    print("oldI \(oldI)")
                    print("i = \(i)")
                    print("Getting objects")
                    // self.productImages.append((object["productImage\(i)"] as? PFFile)!)
                    print("oldI = i")
                    oldI++
                }
            }
        }
    }


}

func printValue(result: Bool) {
    print("result = \(result)")
}

func returnImage(pictureFile: PFFile, let currentIteration: Int, completion: (result: Bool) -> Void) {
    pictureFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
        if error != nil {
            print(error)
            completion(result: false)
        } else {
            if let data = imageData {
                print("currentIteration = \(currentIteration)")
                //var circleArray = Array<UIImageView>()
                //var y : CGFloat = 0.0
                //var i = 0
                self.imageViewArray.append(UIImageView(frame:CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 80), 120, 120)))
                print("Number Of Images Count = \(self.numberOfImages)")
                print("ImageViewArray Count = \(self.imageViewArray.count)")

                self.imageView = self.imageViewArray[currentIteration] // This is the line where the Array index out of range occurs

                self.imageView.image = UIImage(data: data)!
                self.imageView.tag = currentIteration
                self.view.addSubview(self.imageView)

                //self.imageView[i].frame = CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 10), 60, 60)
                //self.imageView[i].image = UIImage(data: data)!
                self.imageView.contentMode = UIViewContentMode.ScaleAspectFill
                self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
                self.imageView.clipsToBounds = true

            }
        }

        completion(result: true)
    })
}


  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
}


这是存储对象的后端,正在检索两个图像文件
ios - 数组索引超出范围跳过功能-LMLPHP

最佳答案

根据您当前的逻辑,尝试使用以下代码添加ImageView,

self.imageViewArray.append(UIImageView(frame:CGRectZero))


因为如果发生错误,则阵列中用于下一次迭代的图像数量会减少。
例如如果在迭代3中发生错误,那么对于下一个迭代4,如果迭代4成功,则将只有3张图像。

10-01 14:09