【发布时间】:2018-12-08 13:39:47
【问题描述】:
所以,我一直在构建这个项目,它是一个待办事项应用程序。当你点击一个单元格时,你的项目应该被搜索到 ask.com。我现在不断收到这个致命错误。正如您在我的代码中看到的那样,它在我的代码的“Appurl”部分中显示为 nil。当我在代码中单击它时,它显示为 nil,这很奇怪。此外,它导致我的应用程序崩溃。源代码将是惊人的。我不知道如何解决这个问题。我所知道的是零出现在“Appurl”中出现的致命错误消息如下。我已经为这种类型的致命错误消息查找了其他答案,但没有运气。 “线程 1:致命错误:在展开可选值时意外发现 nil”
import UIKit
class NewTableViewController: UITableViewController, NewCellDelegate, {
var news:[News]!
override func viewDidLoad() {
super.viewDidLoad()
loadData()
func loadData() {
news = [News]()
news = DataManager.loadAll(News.self).sorted(by: {$0.createdAt < $1.createdAt})
self.tableView.reloadData()
}
@IBAction func Save(_ sender: Any) {
let addAlert = UIAlertController(title: "ADD", message: "TODO", preferredStyle: .alert)
addAlert.addTextField { (textfield:UITextField) in
textfield.placeholder = "TODO"
}
addAlert.addAction(UIAlertAction(title: "Save", style: .default, handler: { (action:UIAlertAction) in
guard let title = addAlert.textFields?.first?.text else {return}
let newsave = News(title: title, completed: false, createdAt: Date(), itemIdentifier: UUID())
newsave.saveItem()
self.news.append(newsave)
let indexPath = IndexPath(row: self.tableView.numberOfRows(inSection: 0), section: 0)
self.tableView.insertRows(at: [indexPath], with: .automatic)
}))
addAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(addAlert, animated: true, completion: nil)
}
};
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return news.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewTableViewCell
cell.delegte = self
let news = self.news[indexPath.row]
cell.label.text = news.title
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
//getting the index path of selected row
let indexPath = tableView.indexPathForSelectedRow
//getting the current cell from the index path
let currentCell = tableView.cellForRow(at: indexPath!) as! NewTableViewCell
//getting the text of that cell
let TODO = currentCell.label.text
let appURL = NSURL(string: "https://www.ask.com/web?q=\
(TODO))&o=0&qo=homepageSearchBox)")
if UIApplication.shared.canOpenURL(appURL! as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL! as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL! as URL)
}
}
}
【问题讨论】:
-
appURL是nil因为我猜它有空格或特殊字符。寻找百分比转义?另外,在 Swift3 中一般不要使用NSURL或NSStuff。如果可用,请使用 Swift 类(NSURL=>URL等)。 -
@Larme 没有使用特殊字符?
标签: ios swift xcode tableview fatal-error