[英]“Array index out of range” when choosing photos in Swift
I cant figure out what's the problem here. 我不知道这是什么问题。
I have 6 images and when i click on them it popsup a image picker. 我有6张图片,当我点击它们时,它会弹出一个图片选择器。 If i choose a photo with a sequence, like 1,2,3,4,5,6... it works. 如果我选择带有序列的照片,例如1,2,3,4,5,6 ...,它会起作用。 But when i choose the first photo that's not the first one (array[0]), i get an error: 但是,当我选择的照片不是第一张照片(array [0])时,出现错误:
`$"Array index out of range" $“数组索引超出范围”
Here is my code: 这是我的代码:
class SecondViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagemEscolhida = -1
@IBOutlet var firstImageView: UIImageView!
@IBOutlet var secondImageView: UIImageView!
@IBOutlet var thirdImageView: UIImageView!
@IBOutlet var fourthImageView: UIImageView!
@IBOutlet var fifthImageView: UIImageView!
@IBOutlet var sixthImageView: UIImageView!
@IBAction func firstImageButton(sender: AnyObject) {
imagemEscolhida = 1
abreBibliotecaFotos(imagemEscolhida)
}
@IBAction func secondImageButton(sender: AnyObject) {
imagemEscolhida = 2
abreBibliotecaFotos(imagemEscolhida)
}
@IBAction func thirdImageButton(sender: AnyObject) {
imagemEscolhida = 3
abreBibliotecaFotos(imagemEscolhida)
}
@IBAction func fourthImageButton(sender: AnyObject) {
imagemEscolhida = 4
abreBibliotecaFotos(imagemEscolhida)
}
@IBAction func fifthImageButton(sender: AnyObject) {
imagemEscolhida = 5
abreBibliotecaFotos(imagemEscolhida)
}
@IBAction func sixthImageButton(sender: AnyObject) {
imagemEscolhida = 6
abreBibliotecaFotos(imagemEscolhida)
}
func abreBibliotecaFotos (img: Int )
{
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
let imagePicker = UIImagePickerController()
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
firstImageView.contentMode = .ScaleAspectFit
restaurante.imagem.insert(pickedImage, atIndex: imagemEscolhida-1)
switch imagemEscolhida
{
case 1:
firstImageView.image = restaurante.imagem[0]
case 2:
secondImageView.image = restaurante.imagem[1]
case 3:
thirdImageView.image = restaurante.imagem[2]
case 4:
fourthImageView.image = restaurante.imagem[3]
case 5:
fifthImageView.image = restaurante.imagem[4]
case 6:
sixthImageView.image = restaurante.imagem[5]
default:
println("Something else")
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
}
It looks like the restaurante.imagem
array is empty and your code crashes because you try to insert something at index which is higher than the maximum index of the array - which is 0
. 看起来restaurante.imagem
数组为空,并且代码崩溃,因为您尝试在索引处插入比该数组的最大索引(即0
。
The line to blame is: 怪罪的行是:
restaurante.imagem.insert(pickedImage, atIndex: imagemEscolhida-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.