Reputation: 49097
I am trying to add a simple annotation to my map. However it crash on the addAnnotation method. What is wrong? I am using Swift. The connection between map and the object in the Interface Builder is correctly set up. All I get is a EXC_BAD_ACCESS
class MyMapAnnotation : NSObject, MKAnnotation {
let title: String
let subtitle: String
let coordinate: CLLocationCoordinate2D
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}
}
And I try this in my view controller:
let coordinate = CLLocationCoordinate2D(latitude: 46.830930, longitude: 7.705106)
let annotation = MyMapAnnotation(title: "Title", subtitle: "Subtitle", coordinate: coordinate)
if CLLocationCoordinate2DIsValid(coordinate) {
map.addAnnotation(annotation)
}
Upvotes: 1
Views: 617
Reputation:
The MKAnnotation
protocol requires the properties to be var
and the strings to be optional (!
).
In the MyMapAnnotation
class, change these declarations:
let title: String
let subtitle: String
let coordinate: CLLocationCoordinate2D
to this:
var title: String!
var subtitle: String!
var coordinate: CLLocationCoordinate2D
Upvotes: 1