数组超出范围 - Swift

时间:2014-10-20 15:53:57

标签: swift

我有一个从Parse获取图像的块:

func getImageData(objects: [PFObject]) {

        let downloadGroup = dispatch_group_create()

        for object in objects {
            let thumbNail = object["StaffPic"] as PFFile
            dispatch_group_enter(downloadGroup)

            thumbNail.getDataInBackgroundWithBlock({
                (imageData: NSData!, error: NSError!) -> Void in
                if (error == nil) {
                    let image = UIImage(data:imageData)
                    self.logoImages.append(image)

                    dispatch_group_leave(downloadGroup)
                }


            }, progressBlock: {(percentDone: CInt) -> Void in


        })




        }

        dispatch_group_notify(downloadGroup, dispatch_get_main_queue()) {

            self.arrayimages.append(self.ImageView0)
            self.arrayimages.append(self.ImageView1)
            self.arrayimages.append(self.ImageView2)
            self.arrayimages.append(self.ImageView3)
            self.arrayimages.append(self.ImageView4)
            self.arrayimages.append(self.ImageView5)
            self.arrayimages.append(self.ImageView6)
            self.arrayimages.append(self.ImageView7)
            self.arrayimages.append(self.ImageView8)


            for var i = 0; i <= self.logoImages.count; i++
            {
                var division : CGFloat = (self.ImageViewMain.frame.size.width / 2.0)
                self.arrayimages[i].image = self.logoImages[i]
                self.arrayimages[i].layer.cornerRadius = division
                self.arrayimages[i].clipsToBounds = true
                self.arrayimages[i].layer.borderWidth = 2.0
                self.arrayimages[i].layer.borderColor = self.colour.CGColor
                self.view.addSubview(self.arrayimages[i])
            }

当所有图像下载时,它会运行dispatch_group_notify,并在此块中将数组图像添加到UIImageView数组中。但我得到致命错误:数组超出范围。 有什么建议?

2 个答案:

答案 0 :(得分:5)

我会说错误在这里:

for var i = 0; i <= self.logoImages.count; i++

我认为上限应该是<

for var i = 0; i < self.logoImages.count; i++

你能检查一下吗?

答案 1 :(得分:1)

也许改变这一行:

for var i = 0;我&lt; = self.logoImages.count

for var i = 0;我&lt; self.logoImages.count

您的数组计数为10,那么您的范围必须为0 ... 9且10将超出范围。