gpt4 book ai didi

json - 可选值的包装问题

转载 作者:行者123 更新时间:2023-11-30 12:36:59 25 4
gpt4 key购买 nike

我对 Swift 还很陌生,目前在包装可选值方面遇到一些问题。从我的博客下载 JSON(下载工作正常)后,我的应用程序崩溃并出现错误:fatal error: unexpectedly found nil while unwrapping an Optional value

我不知道如何解决这个问题。这是代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!

var postsarray : [SDPost] = []
var hasloaded = false
var HUD = MBProgressHUD()

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.register(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

HUD = MBProgressHUD.showAdded(to: self.view, animated: true)
HUD.detailsLabel.text = "Daten werden geladen"
HUD.label.text = "Moment bitte."

self.tableView.register(TableCellTableViewCell.self, forCellReuseIdentifier: "cell")
}
override func viewDidAppear(_ animated: Bool) {
let feed = SDFeedParser();
let url = "www.pathtomyblog.net"

tableView.isHidden = true
feed.parseURL(url, success: { (Array:[Any]?, Int) in
if (Array != nil)
{
self.postsarray = Array as! [SDPost]
self.tableView.isHidden = false
self.hasloaded = true
self.tableView.reloadData()
print("Data loaded.")
}
self.HUD.hide(animated: true, afterDelay: 0.1)
}) { (Error) in
print("Something went wrong." + (Error?.localizedDescription)!)
}

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.postsarray.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:TableCellTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as! TableCellTableViewCell

if (hasloaded){
var post_info = SDPost()
post_info = postsarray[indexPath.row]
cell.articel_title.text = post_info.title;

return cell
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
}

希望你能帮助我。预先致以最诚挚的问候和感谢!

最佳答案

首先尝试阅读有关 Swift 语言的内容 syntaxes 。使用力解开!如果你不检查你正在打开或类型转换的东西,这是非常危险的。

尝试使用这种基本的语言方法:

if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}

guard let image = UIImage(named: imageName) else {
return
}

关于json - 可选值的包装问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42745452/

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