数组索引超出范围跳过功能

时间:2016-01-06 05:33:31

标签: ios arrays swift loops parse-platform

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

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++
                }

这是控制台打印出的内容

  1. 成功
  2. 关于businessDetailsVC
  3. 图像数量2
  4. 对象数量可选(1)
  5. 可选(&#34; H1b7iUj4PS&#34)
  6. oldI -1
  7. i = 0
  8. 获取对象
  9. oldI = i
  10. 可选(&#34; H1b7iUj4PS&#34)
  11. oldI -1
  12. i = 1
  13. 获取对象
  14. oldI = i
  15. currentIteration = 1
  16. 图像数量= 2
  17. ImageViewArray Count = 1
  18. 致命错误:数组索引超出范围 (lldb)
  19. 这是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.
      }
    }
    

    这是存储对象的后端,检索到两个图像文件 This is the backend where the object is stored, with two image files being retrieved

1 个答案:

答案 0 :(得分:0)

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

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

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