ios - 如何快速修复 "Cannot subscript a value of type ' ClosedRange<Int >' with an index of type ' Int'

标签 ios arrays swift countdowntimer numberpicker

我想为我的家庭作业准备一个基本的数字选择器计时器,但我无法解决这个问题。

var timer = Timer()
    var counter = 0

    //let number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"]

    let number = 1 ... 60

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

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return number[row]
    }




    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return number.count
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        label.text = number[row]

    } 

但它给出了这个错误。

Cannot subscript a value of type 'ClosedRange' with an index of type 'Int'

最佳答案

您正在创建一个数组,因此需要用方括号括住该范围。

let numbers = [1...60]

关于ios - 如何快速修复 "Cannot subscript a value of type ' ClosedRange<Int >' with an index of type ' Int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609138/

相关文章:

c# - Monotouch (Xamarin.iOS) 内存泄漏

c - 大小未知的动态数组

javascript - 如何根据重复键合并/组合对象数组?

javascript - 如何仅使用 Javascript 一次显示一个数组元素?

swift - VC模态呈现时导航栏样式不会改变

ios - 如何确保我的 for in 循环使我只能执行一个 segue?

ios - Workplace by Facebook iPhone 应用程序支持哪些自定义 URL 方案?

ios - 如何在不包装旧库的情况下让 OAuth2 与 SwiftUI 一起使用?

swift - 在TableView Swift 4中显示Firebase数据

objective-c - 使用 Objective-c typedef 快速关闭的可变参数