gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:48:25 25 4
gpt4 key购买 nike

当我尝试循环执行以下代码时,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++
}

这是控制台打印出来的内容

  1. 成功
  2. 关于业务详细信息VC
  3. 图像数量 2
  4. 对象数量可选(1)
  5. 可选(“H1b7iUj4PS”)
  6. 旧我-1
  7. i = 0
  8. 获取对象
  9. 旧我=我
  10. 可选(“H1b7iUj4PS”)
  11. 旧我-1
  12. i = 1
  13. 获取对象
  14. 旧我=我
  15. 当前迭代 = 1
  16. 图像数量 = 2
  17. ImageViewArray 计数 = 1
  18. fatal error :数组索引超出范围(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.
}
}

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

最佳答案

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

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

因为如果发生错误,下一次迭代时数组中的图像数量将会减少。例如如果在迭代 3 中发生错误,那么对于下一次迭代 4,如果迭代 4 成功,您将仅获得 3 个图像。

关于ios - 数组索引超出范围跳过功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34626276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com