Swift:不能下标类型'数组'

时间:2016-02-08 04:26:05

标签: swift

我刚开始学习swift ios开发。我遇到了这个我无法解决的问题。谁能告诉我我做错了什么?下图包含错误的屏幕截图。

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var dayPicker: UIPickerView!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var textField: UITextField!
    let days = ["Sunday","Monday","Tuesday","Thursday","Friday","Saturday"]
    // creates an array of 7 empty arrays
    var tableData:Array = [[String]] (count: 7,repeatedValue:[])
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func addObjectToTable(sender: AnyObject) {
        if(textField.text != "")
        {
            print (days[dayPicker.selectedRowInComponent(0)])
            print (textField.text!)
            tableData[dayPicker.selectedRowInComponent(0)].append(textField.text!); // Error: Cannot subscript a value of type 'Array'
            textField.text = ""
            tableView.reloadData()
        }
    }
}

Screenshot

谢谢!

2 个答案:

答案 0 :(得分:2)

我记得当我回到一些有点过时的旧项目时,我遇到过这个问题几次。对我来说,修复只是为了改变,在你的例子中,

var tableData:Array = [[String]]

var tableData = [[String]]

答案 1 :(得分:0)

如果您确定tableview只包含几天,请尝试将对象Array of AnyObject类型转换为String of Array。然后使用它。

Use custom workspace