gpt4 book ai didi

ios - Swift:无法将我的值放入标签(可选问题)

转载 作者:行者123 更新时间:2023-11-30 10:58:57 24 4
gpt4 key购买 nike

我从 Firebase 获得了自己的值(value),但 Swift 不想将其放入我的标签中。

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

总而言之,我的数据库如下所示: Firebase database

我创建了一个标准模型调用 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
}

}

现在,这是我的 View 的 Controller :

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()
}
})
}

当我启动此 View 时,出现前面提到的错误。当我调试时,我发现 service.name、service.category 和 service.pricing 中有正确的值

看来我没有正确处理可选值,但我看不出哪里出了问题。

感谢您的帮助。

最佳答案

在可选展开的情况下可能崩溃的行是这一行

    refServices = Database.database().reference().child("Categories");
refServices.observe(DataEventType.value, with: { (snapshot) in

尝试设置断点并检查 refServices 是否已正确初始化,或者不使用 将 ti 设为可选!

希望这有帮助

诗。请从您的 Swift 代码中删除 ; :P

关于ios - Swift:无法将我的值放入标签(可选问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53628842/

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