填充表格视图时,我的应用程序崩溃,因为它声称 属性 为零,但在我设置它之前打印 属性 说它有一个值
When populating a tableview, my app crashes because it claims that a property is nil, but printing that property before I set it says it has a value
我正在使用搜索栏搜索用户名数据库,并相应地更新表视图的 User() 数据库。搜索可以很好地更新数组,但是当我的 tableview 试图将其自己的标签更改为 User() 的名字时,我的应用程序崩溃了,因为它声称 firstName 为 nil。不过,我知道事实并非为零。在尝试更新标签之前,我打印对象的 属性 并打印正确的用户名。我不知道我做错了什么。这是代码...
class AddFriendsVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var profiles = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profiles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? FriendCell {
let friend = profiles[indexPath.row]
print(friend.firstName) //PRINTS THE USERNAME...
cell.nameLabel.text = friend.firstName //CRASHES HERE. firstName is of type "String!"
return cell
}
return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
DataService.dataService.REF_USERNAMES.queryOrderedByKey().queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
var results = [User]()
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
print(snap.key)
let result = User(first: "\(snap.key)", last: "s", username: "s", activeTrip: "s")
results.append(result)
if snap == snapshots.last {
self.profiles = results
print(self.profiles[0].firstName) //PRINTS CORRECT USERNAME
self.tableView.reloadData()
}
}
}
}
})
}
}
每次我打印数据时,用户名都在那里...为什么这不起作用?
问题不是 friend.firstName
,而是 cell.nameLabel
。 IBOutlet
s 是隐式展开的可选值,在本例中为 UILabel!
,这意味着如果它们未正确连接,它们将是 nil
并像您看到的那样崩溃。确保 属性 连接到 class 并且它应该是固定的。
我正在使用搜索栏搜索用户名数据库,并相应地更新表视图的 User() 数据库。搜索可以很好地更新数组,但是当我的 tableview 试图将其自己的标签更改为 User() 的名字时,我的应用程序崩溃了,因为它声称 firstName 为 nil。不过,我知道事实并非为零。在尝试更新标签之前,我打印对象的 属性 并打印正确的用户名。我不知道我做错了什么。这是代码...
class AddFriendsVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var profiles = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profiles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? FriendCell {
let friend = profiles[indexPath.row]
print(friend.firstName) //PRINTS THE USERNAME...
cell.nameLabel.text = friend.firstName //CRASHES HERE. firstName is of type "String!"
return cell
}
return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
DataService.dataService.REF_USERNAMES.queryOrderedByKey().queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
var results = [User]()
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
print(snap.key)
let result = User(first: "\(snap.key)", last: "s", username: "s", activeTrip: "s")
results.append(result)
if snap == snapshots.last {
self.profiles = results
print(self.profiles[0].firstName) //PRINTS CORRECT USERNAME
self.tableView.reloadData()
}
}
}
}
})
}
}
每次我打印数据时,用户名都在那里...为什么这不起作用?
问题不是 friend.firstName
,而是 cell.nameLabel
。 IBOutlet
s 是隐式展开的可选值,在本例中为 UILabel!
,这意味着如果它们未正确连接,它们将是 nil
并像您看到的那样崩溃。确保 属性 连接到 class 并且它应该是固定的。