Map Annotations

the following code from Page 198
func updateLocations() {
mapView.removeAnnotations(locations)
let entity = Location.entity()
let fetchRequest = NSFetchRequest()
fetchRequest.entity = entity
locations = try! managedObjectContext.fetch(fetchRequest)
mapView.addAnnotations(locations)
}
always returns a bug, I have rewritten this manny times now but I am forced by Xcode to add a cast to annotations in the mapView call and it also makes me add public before the coded added in locations. it then fails tell me that you cannot cast type location to annotations…

any ideas?

That is not what the code on p.198 says. It says this:

let fetchRequest = NSFetchRequest<Location>()

not

let fetchRequest = NSFetchRequest()

Hi

Sorry you are correct - but that was just a typo by me on the copy, I still get the error -
Could not cast value of type ‘Swift._EmptyArrayStorage’ (0x1004c66b8) to ‘MKAnnotation’ (0x10269c0b0).
when I compile it wants me to add a cast as! MKAnnotation
when I run it crashes on line
map.removeAnnotation(locations as! MKAnnotation)

any ideas?

James

I can’t really tell without seeing the actual code. Can you zip your project folder and upload it somewhere, so I can take a look?

sure, the file right now is very small, the code for the map display is very small and this is the only area I am having some issue
I could just cut and paste the text here
or email it to you if you prefer

James

I really need a zip file of the entire project so I can compile it for myself and run it. Otherwise it can take forever to find the problem…

Hi

Sorry for the delay - unfortunately I was taken ill and have only just got… I have found the problem, typo, however Xcode still make me put “public” in front of the local variables " public var title: String? {"… the reason for the my major failure was a missing ‘s’… the original code is removeAnnotations(locations) - let Xcode help me type, I don’t copy paste as I believe I learn more by actually typing in what I want…anyway I end up with “removeAnnotation(locations)”… very nasty, both function are valid one looks for an array, one looks for a single entity… I was passing an array to the wrong one… hence the statement that it did not conform and need to be cast… anyway thanks for your help and again sorry for the delay in getting back to you.

James

1 Like

It’s true, sometimes small differences in the code matter a lot. :wink: