请原谅我提出了一个简单的问题,因为我已经阅读了很多解决方案,但仍然不知道如何解决。
如果按钮在同一类中,则可以成功运行TutorListViewController上的以下代码,并调用函数observeEnglishCases()。但是,当我尝试在不同的类和视图控制器中执行此操作时,当我在刺激器中轻按按钮时,应用程序崩溃并显示以下代码:线程1:致命错误:在展开一个可选值(并且它不是重复的问题)
有人会在几天后未能做到这一点让我难过吗。
这是ViewController1上的代码:
@IBAction func englishGO(_ sender: Any) {
self.tabBarController?.selectedIndex = 1
let tutorVC = storyboard?.instantiateViewController(withIdentifier: "TutorListViewController") as? TutorListViewController
tutorVC!.observeEnglishCases()
}
以下是TutorListViewController上的代码:
override func viewDidLoad() {
super.viewDidLoad()
observeEnglishCases()
}
func observeEnglishCases(){
let casesRef = Database.database().reference().child("cases")
let queryRef = casesRef.child("school").queryEqual(toValue: "ling")
queryRef.observe(.value, with: { snapshot in
var tempPosts = [Case]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let name = dict["name"] as? String,
let gender = dict["gender"] as? String,
let school = dict["school"] as? String,
let year = dict["year"] as? String,
let major = dict["major"] as? String,
let tutorsubject = dict["tutorsubject"] as? String,
let tutoryear = dict["tutoryear"] as? String,
let tutorperformance = dict["tutorperformance"] as? String,
let region = dict["region"] as? String,
let minimumprice = dict["minimumprice"] as? String,
let thingtospeak = dict["thingtospeak"] as? String,
let photoURL = dict["photoURL"] as? String,
let url = URL(string:photoURL){
let caser = Case(name: name, gender: gender, school: school, year: year, major: major, tutorsubject: tutorsubject, tutoryear: tutoryear, tutorperformance: tutorperformance, region: region, minimumprice: minimumprice, thingtospeak: thingtospeak, photoURL: url)
tempPosts.insert(caser, at:0)
}
}
self.cases = tempPosts
self.tutorCasesCollectionView.reloadData() //App Crashed Here
})
}