快速-致命错误:数组索引超出范围

时间:2022-07-21 16:38:34

I'm trying to populate an array with numbers with an increase of 0.1 each like: [0.1,0.2,0.3...]

我试图用数字填充一个数组,每个数字增加0.1,比如[0.1,0.2,0.3…]

This code is giving me the error: fatal error: Array index out of range . What am I missing? I thing im declaring something wrong.

这段代码给了我一个错误:致命错误:数组索引超出范围。我缺少什么?我想我说错了什么。

I will save it into a structure of type Double.

我将它保存到Double类型的结构中。

My Code

我的代码

import UIKit

class PrecoDomicilioViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource{

@IBOutlet var euros: UIPickerView!

var pickerData:[Double] = []


override func viewDidLoad() {
    super.viewDidLoad()

    euros.delegate = self
    euros.dataSource = self




    for var i = 0; i <= 200; i++
    {
        pickerData[i] += 0.1
    }

}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return pickerData.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return "\(pickerData[row])"
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    restaurante.portes = pickerData[row]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


}
}

UPDATE: Finally made it, but there are this strange numbers between 6 and 10.

更新:最终成功了,但是有这个奇怪的数字在6到10之间。

enter image description here 快速-致命错误:数组索引超出范围

在这里输入图像描述

1 个解决方案

#1


2  

You can not add items to a Swift array with the subscript operator, you need to use append.

不能使用下标操作符向Swift数组添加项,需要使用append。

NOTE You can’t use subscript syntax to append a new item to the end of an array.”

注意,不能使用下标语法将新项附加到数组的末尾。

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

摘自:苹果公司“Swift编程语言”(Swift 2预发行)。“iBooks。https://itun.es/us/k5SW7.l

Use append(), examples:

使用append()例子:

shoppingList.append("Flour")
pickerData.append(0.1)

#1


2  

You can not add items to a Swift array with the subscript operator, you need to use append.

不能使用下标操作符向Swift数组添加项,需要使用append。

NOTE You can’t use subscript syntax to append a new item to the end of an array.”

注意,不能使用下标语法将新项附加到数组的末尾。

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

摘自:苹果公司“Swift编程语言”(Swift 2预发行)。“iBooks。https://itun.es/us/k5SW7.l

Use append(), examples:

使用append()例子:

shoppingList.append("Flour")
pickerData.append(0.1)