在 Swift 中解包可选时发现 nil
found nil while unwrapping optional in Swift
我知道这可能是一个简单的问题,只是不确定我在这里缺少什么需要一双新的眼睛。我在这里明白我需要解开我正在尝试做的事情,但它一直失败,我做错了什么有什么帮助吗?这是代码(我可能会踢自己)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var currentLocation = self.locations[indexPath.row]
var displayLocation = currentLocation["User"] as! PFObject
let cell = UITableViewCell()
if let display = displayLocation as PFObject? {
cell.textLabel?.text = display["currentLocation"] as? String
}
return cell
}
对不起大家忘了提到它在这一行上的失败
var displayLocation = currentLocation["User"] as! PFObject
currentLocation
字典缺少关键字 "User" 的值,或者值为 nil。
该错误意味着您正在将 currentLocation["User"]
转换为 PFObject
和 as!
,这是不可能的。因为可能你的 currentLocation["User"]
是 nil
.
如果是 nil
,则不能将其转换为 PFObject
。
保持简单,就像这样
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var currentLocation = self.locations[indexPath.row]
let cell = UITableViewCell()
if let displayLocation = currentLocation["User"] as? PFObject {
cell.textLabel?.text = displayLocation["currentLocation"] as? String
}
return cell
}
我知道这可能是一个简单的问题,只是不确定我在这里缺少什么需要一双新的眼睛。我在这里明白我需要解开我正在尝试做的事情,但它一直失败,我做错了什么有什么帮助吗?这是代码(我可能会踢自己)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var currentLocation = self.locations[indexPath.row]
var displayLocation = currentLocation["User"] as! PFObject
let cell = UITableViewCell()
if let display = displayLocation as PFObject? {
cell.textLabel?.text = display["currentLocation"] as? String
}
return cell
}
对不起大家忘了提到它在这一行上的失败
var displayLocation = currentLocation["User"] as! PFObject
currentLocation
字典缺少关键字 "User" 的值,或者值为 nil。
该错误意味着您正在将 currentLocation["User"]
转换为 PFObject
和 as!
,这是不可能的。因为可能你的 currentLocation["User"]
是 nil
.
如果是 nil
,则不能将其转换为 PFObject
。
保持简单,就像这样
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var currentLocation = self.locations[indexPath.row]
let cell = UITableViewCell()
if let displayLocation = currentLocation["User"] as? PFObject {
cell.textLabel?.text = displayLocation["currentLocation"] as? String
}
return cell
}