Ich bin neu in iOS. Doing-Projekt durch Anschauen von Tutorial, das mit Swift 2 geschrieben wurde.
Es funktioniert, wenn der Autor App, aber nicht in meinem Fall läuft.Swift 3: Index außerhalb des Bereichs Fehler
Viewcontroller:
var books = [[String: AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
}
Und die Erweiterung
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BookCell", for: indexPath)
// error on this line
if let volumeInfo = self.books[indexPath.row]["volumeInfo"] as? [String: AnyObject] {
cell.textLabel?.text = volumeInfo["title"] as? String
cell.detailTextLabel?.text = volumeInfo["subtitle"] as? String
}
return cell
}
}
Die Konsolenausgabe:
Bitte, mir helfen zu erkennen, was die Ursache ist.
Doubt es etwas mit Swift zu tun hat 3 vs 2. Ihr 'books' Array wahrscheinlich leer ist (oder zumindest' indexPath.section > = books.count'). Überprüfen Sie, was sich in 'indexPath' und' books' befindet, und vergewissern Sie sich, dass kein Teil des Codes fehlt, in dem das 'books'-Array initialisiert wurde. – shim
Was geben Sie in 'numberOfRowsInSection' zurück? Das 'Bücher'-Array ist offensichtlich leer. – vadian