ios - Swift:无法将我的值放入标签(可选问题)
问题描述
我从 Firebase 获得了我的价值,但 Swift 不想把它放在我的标签中。
线程 1:致命错误:在展开可选值时意外发现 nil
总而言之,我的数据库如下所示: Firebase 数据库
我创建了一个标准模型调用 ServiceModel:
import Foundation
class ServiceModel {
var name: String?
var category: String?
var pricing: String?
init(name: String?, category: String?, pricing: String?){
self.name = name
self.category = category
self.pricing = pricing
}
}
我想将这些值显示到 TableView 中,所以我创建了一个这样的自定义单元格(也非常标准):
import UIKit
class SubscriptionTableViewCell: UITableViewCell {
@IBOutlet weak var imageService: UIImageView!
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var labelCategory: UILabel!
@IBOutlet weak var labelPricing: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
现在,这是我的视图的控制器:
import UIKit
import FirebaseDatabase
class SecondViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
var refServices:DatabaseReference!
@IBOutlet weak var ListSub: UITableView!
var serviceList = [ServiceModel]()
var databaseHandle:DatabaseHandle?
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return serviceList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath) as! SubscriptionTableViewCell
let service: ServiceModel
service = serviceList[indexPath.row]
//cell.imageService.image = UIImage(named: service.name! + ".png")
cell.labelName?.text = service.name //ERROR HERE
cell.labelCategory?.text = service.category
cell.labelPricing?.text = service.pricing
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
ListSub.delegate = self
ListSub.dataSource = self
refServices = Database.database().reference().child("Categories");
refServices.observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
self.serviceList.removeAll()
for services in snapshot.children.allObjects as! [DataSnapshot] {
let serviceObject = services.value as? [String: AnyObject]
let serviceName = serviceObject?["Name"]
let serviceCategory = serviceObject?["Category"]
let servicePricing = serviceObject?["Pricing"]
let service = ServiceModel(name: serviceName as! String?, category: serviceCategory as! String?, pricing: servicePricing as! String?)
self.serviceList.append(service)
}
self.ListSub.reloadData()
}
})
}
当我启动这个视图时,我遇到了前面提到的错误。当我调试时,我发现我在 service.name、service.category 和 service.pricing 中有正确的值
似乎我没有正确处理 Optional 值,但我看不出有什么问题。
谢谢你的帮助。
解决方案
If you debug this function, what did you see for your service
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath) as! SubscriptionTableViewCell
let service: ServiceModel
//Put a breakpoint here
service = serviceList[indexPath.row]
//Put a breakpoint here
//cell.imageService.image = UIImage(named: service.name! + ".png")
cell.labelName?.text = service.name
cell.labelCategory?.text = service.category
cell.labelPricing?.text = service.pricing
return cell
}
推荐阅读
- bash - Bash:从带有空格的wsl参数正确传递给win10主机上的程序
- java - 在第一个活动中找不到第二个活动
- ruby-on-rails - Rails:具有通用布局的渲染模式
- java - 有没有办法让玩家在坐标中给出特定的物品?
- sql-server - sql server中ð的重音不敏感排序规则
- node.js - 不使用 listobject 的 AWS S3 复制文件夹
- python - 在 Pandas Column 中搜索每行值包含列表的字符串
- ruby-on-rails - Postgres 数组格式错误的数组字面量
- c# - 为 JsonSerializer.DeserializeAsync 动态创建一个类型?
- python - Tkinter 无法配置