class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = "hi!"
return cell
}
}
let mapViewController = segue.destination as! MapViewController
class MapViewController: UIViewController {
var activePlace = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
as! MapViewController
guard let mapViewController = segue.destination as? MapViewController else {
//Aqui tratas a eventualidade de o mapViewController não ser um MapViewController, o downside desta abordagem é que o guard tem que saltar fora, pode não ser o melhor em todo o lado
return
}
// aqui já sabes que mapViewController tem o tipo que querias e podes usá-lo livremente
if segue.destination is MapViewController {
// Sabes que é
} else {
// sabes que não é
}
if segue.destination is MapViewController {
// Sabes que é
let mapViewController = segue.destination
} else {
// sabes que não é
Print("erro MapViewController não existe")
}
guard let mapViewController = segue.destination as? MapViewController else {
return
}
// A partir daqui sabes que o mapViewController existe e é obrigatoriamente um MapViewController
if let mapViewController = segue.destination as? MapViewController {
// Aqui dentro sabes que o mapViewController existe e é obrigatoriamente um MapViewController
} else {
}
//Aqui já não conheces o mapViewController, ele apenas está vivo dentro do if
places.append(["name":title,"lat":String(newCoordinate.latitude),"lon":String(newCoordinate.longitude)])
print(places)
})
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error)
in
if error != nil{
print(error)
}
else{
(...)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
self.map.addAnnotation(annotation)
places.append(["name":title,"lat":String(newCoordinate.latitude),"lon":String(newCoordinate.longitude)])
})