我修复了原来的问题,但是我有一个新的错误,上面写着“Type'ViewController'不符合协议'SideBarDelegate'。”
我知道我非常接近这个工作,但由于某种原因无法弄清楚为什么会这样!有人可以帮忙吗?
class ViewController: UIViewController, SideBarDelegate {
var sideBar: SideBar = SideBar()
@IBOutlet weak var mapView: MKMapView!
let locationsRef = FIRDatabase.database().reference(withPath: "locations")
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
//sidebar
sideBar = SideBar(sourceView: self.view, skateItems: ["All Skate Spots", "Skateparks", "Street Skating"])
// Passing firebase annotation data
locationsRef.observe(.value, with: { snapshot in
for item in snapshot.children {
guard let locationData = item as? FIRDataSnapshot else { continue }
let locationValue = locationData.value as! [String: Any]
var location: CLLocationCoordinate2D!
if let lat = locationValue["lat"] as? String {
// Stored as String
let lng = Double(locationValue["lng"] as! String)!
location = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: lng)
}
else {
// Stored as a floating point number
let lat = locationValue["lat"] as! Double
let lng = locationValue["lng"] as! Double
location = CLLocationCoordinate2D(latitude: lat, longitude: lng)
}
let name = locationValue["name"] as! String
let subtitle = locationValue["subtitle"] as! String
self.addAnnotation(at: location, name: name, subtitle: subtitle)
}
})
}
func addAnnotation(at location: CLLocationCoordinate2D, name: String, subtitle: String) {
// Adding annotation to the map
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotation.title = name
annotation.subtitle = subtitle
mapView.addAnnotation(annotation)
}
}
func sideBarDidSelectButtonAtIndex(index: Int) {
}
extension ViewController: MKMapViewDelegate {
}
extension ViewController: CLLocationManagerDelegate {
}
答案 0 :(得分:0)
通常委托被声明为弱可选(解决了未初始化的错误)
weak var delegate: SideBarDelegate?
并且始终使用可选链接调用,如果未设置委托,则不执行任何操作。
例如
delegate?.sideBarWillClose?()