I am trying to learn Xcode using iOS 14 Programming for Beginners (Packt Pub).
I am now working on the part that adds the pins to the MapView, but when I run the app the map button is unresponsive (Runtime error).
I get the following message in the Debug area: __2021-07-25 16:08:23.919240+0100 EatOut[1420:21167] Metal API Validation Enabled
Could not cast value of type 'Swift.Array<EatOut.RestaurantItem>' (0x1c31e3588) to '__C.MKAnnotation' (0x1c31e3888).
2021-07-25 16:08:24.003987+0100 EatOut[1420:21167] Could not cast value of type 'Swift.Array<EatOut.RestaurantItem>' (0x1c31e3588) to '__C.MKAnnotation' (0x1c31e3888). Could not cast value of type 'Swift.Array<EatOut.RestaurantItem>' (0x1c31e3588) to '
I'm sure that this message means a lot to you experts, but as a beginner I haven't a clue! The runtime error is in this func, in the final line:
func addMap(_ annontations: [RestaurantItem]) {
mapView.setRegion(manager.currentRegion(latDelta: 0.5, longDelta: 0.5), animated: true)
mapView.addAnnotation(manager.annotations as! MKAnnotation)
I get a build error on the final line if I do not apply the fix, which adds the as! MKAnnotation. Is the error in the .plist or does the code need altering? Please can you help?
Here is the full code for the swift file:
// MapViewController.swift
// EatOut
//
// Created by Tony Hudson on 15/07/2021.
//
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
let manager = MapDataManager()
var selectedRestaurant: RestaurantItem?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
initialise()
func initialise() {
manager.fetch { (annotations) in addMap(annotations)
}
}
func addMap(_ annontations: [RestaurantItem]) {
mapView.setRegion(manager.currentRegion(latDelta: 0.5, longDelta: 0.5), animated: true)
mapView.addAnnotation(manager.annotations as! MKAnnotation)
}
}
}