将 Firebase/Firestore 添加到我的 pod 文件并运行 pod 安装我的应用后,报告 4 在下面的代码中模棱两可地使用"下标(_:("消息:让 monToFri,让坐,让纬度和让经度。如何纠正这种情况?
func displayAnnotations() {
if Reachability.isConnectedToNetwork() {
let ref = Database.database().reference()
ref.child("Postbox").observe(.childAdded, with: { (snapshot) in
let monToFri = (snapshot.value as AnyObject?)!["Monday to Friday Collection Time"] as! String?
let sat = (snapshot.value as AnyObject?)!["Saturday Collection Time"] as! String?
let latitude = (snapshot.value as AnyObject?)!["Latitude"] as! String?
let longitude = (snapshot.value as AnyObject?)!["Longitude"] as! String?
let lastCollection = "Mon - Fri: (monToFri!)" + " Sat: (sat!)"
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: (Double(latitude!))!, longitude: (Double(longitude!))!)
annotation.title = "Last Collection:"
annotation.subtitle = lastCollection
self.mapView.addAnnotation(annotation)
self.allAnnotations.append(annotation)
self.allIds.append(snapshot.key)
self.postboxesLoggedLabel.text = String(self.mapView.annotations.count)
})
} else {
print("Internet Connection not Available!")
let alertController = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
}
为什么每次使用时都下标 snapshot.value?
let ref = Database.database().reference()
ref.child("Postbox").observe(.childAdded, with: { (snapshot) in
let data = snapshot.value as? NSDictionary
let monToFri = data!["Monday to Friday Collection Time"] as? String ?? "Empty String"
// Other code....
}