我剛開始學習swift ios開發。我遇到了這個我無法解決的問題。有人可以告訴我做錯了什麼嗎?下面的圖片包含錯誤的屏幕截圖。Swift:無法爲'Array'類型的值下標
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()
}
}
}
謝謝!
請將您的代碼發佈在答案中,而不是截圖。它看起來像你試圖追加一個字符串到一個數組數組......你可以追加一個數組到數組中,但是你不能把一個字符串追加到一個包含數組的數組中。 – MikeG
添加了代碼。你能否請我在我的代碼中需要更改什麼? – Retr0spect
請指定你正在嘗試做什麼 – Johnykutty