我有从Firebase获取特定帖子的功能
public class main{
public static void main(String[] args){
String response = "Yello";
char c = response.charAt(0);
if(c == 'Y'){
System.out.println("Do something");
}
else if(c == 'y'){
System.out.println("Do another thing");
}
}
}
现在我在
上收到错误 var posts = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
FIRDatabase.database().reference().child("Likes").child(self.loggedInUser!.uid).observe(.childAdded, with: { (snapshot) in
self.ID = snapshot.key
print(self.ID!)
})
loadData()
}
func loadData(){
FIRDatabase.database().reference().child("books").child(self.ID!).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
self.posts.add(post.value)
}
self.SoldTableView.reloadData()
}})
}
说致命错误:在打开可选值时意外发现nil
答案 0 :(得分:0)
看起来你强行打开一个可选的(self.ID),它就是nil,这就是它抛出错误的原因。
在使用值之前,您应该安全地打开可选项:
if let ID = self.ID {
FIRDatabase.database().reference().child("books").child(ID).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
self.posts.add(post.value)
}
self.SoldTableView.reloadData()
}})
}
有关选项的更多信息,请参阅此处: What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?