Dev*_*ech 0 url optional ios firebase swift
这是屏幕截图,您可以看到它显示错误,因为我强制解包并且一些 url 为空:

我怎样才能安全地解开这个 URL,这样我就不必强制解包了?
代码:
func tableView (_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int
{
return players.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
Reusable.reuseIdForMain) as! CustomCell
cell.nameLabel.text = players[indexPath.row].name
cell.otherInfo.text = players[indexPath.row].otherInfo
if let url = players[indexPath.row].imageUrl{
cell.profileImage.load.request(with: URL(string:url)!)
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
检查字符串后,您应该检查 URL 本身的值。两个字符串都将以这种方式安全地展开。
if let urlString = players[indexPath.row].imageUrl,
let url = URL(string: urlString) {
cell.profileImage.load.request(with: url)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3020 次 |
| 最近记录: |