gpt4 book ai didi

ios - fatal error 索引超出范围(Swift 2.3)

转载 作者:行者123 更新时间:2023-12-02 05:32:04 25 4
gpt4 key购买 nike

当我运行我的项目时,我收到 fatal error :索引超出范围,但我找不到解决方案。

非常感谢您的帮助,提前谢谢您!

这是我的代码:

import UIKit

// MARK : Data

var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
"Ku"]

var streets = ["האנגר 28,נמל תל אביב",
"אבן גבירול 30,תל אביב",
"רחוב נמל תל אביב",
"קרליבך 28,תל אביב",
"האנגר 23,נמל תל אביב",
"דרך שלמה 117,תל אביב"]

var images = [UIImage(named: "Shalvata"),
UIImage(named: "Markid"),
UIImage(named: "Litzman Bar"),
UIImage(named: "CatNDog"),
UIImage(named: "LightHouse"),
UIImage(named: "Ku")]

/////////MARK : END Of Data ! ///////////

override func viewDidLoad() {
super.viewDidLoad()

allUsers = createUsers(names: names, streets: streets, images: images)

filteredUsers = allUsers

}

var allUsers: [User]!
var filteredUsers: [User]!

func createUsers(names names: [String], streets: [String], images: [UIImage?]) -> [User] {
var users = [User]()
guard names.count == streets.count && names.count == images.count else { return users }
for (index, name) in names.enumerate() {
let user = User(name: name, streetName: streets[index], image: images[index])
users.append(user)
}
return users
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Search:

if tableView == self.tableView {

return self.names.count
} else {
return self.filteredUsers.count
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user = filteredUsers[indexPath.row]
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName

return cell

}
}

我在行中收到错误:

让用户=filteredUsers[indexPath.row]

错误行:

线程 1:EXC_BREAKPOINT(code=1,子代码=0x10033ddc0)

如果需要任何其他信息,请直接说出来,我正在使用 Xcode 8 和 Swift 2.3,我最近开始编码,所以我是初学者,抱歉;P

最佳答案

因为两个数组可以同时具有不同数量的元素。所以你需要在下面的方法中添加一个条件来基于tableview访问用户。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell

let user:User!

if tableView == self.tableView {
user = allUsers[indexPath.row]
} else {
user = filteredUsers[indexPath.row]
}
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName

return cell
}

关于ios - fatal error 索引超出范围(Swift 2.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41501936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com