Hi!
Im making an app in SwiftUI and following https://www.youtube.com/watch?v=WTzBKOe7MmU tutorial on youtube for maps integration and finding nearby points of interest, in the last part of the video we're making an list of all results of the search. I get an error message "init() is unavailable on iOS" and "'init()' has been explicitly marked unavailable here (MapKit.MKPlacemark)", on this part of the code:
struct PlaceListView_Previews: PreviewProvider {
static var previews: some View {
PlaceListView(landmarks: [Landmark(placemark: MKPlacemark())], onTap: {})
}
}
And this is how my Landmark struct looks like:
struct Landmark {
let placemark: MKPlacemark
var id: UUID {
return UUID()
}
var name: String {
self.placemark.name ?? ""
}
var title: String {
self.placemark.title ?? ""
}
var coordinate: CLLocationCoordinate2D {
self.placemark.coordinate
}
}
It works for my friend but not for me, does somebody know the reason?
According to the documentation, MKPlacemark
doesn't have an initializer that takes no parameters (MKPlacemark()
):
https://developer.apple.com/documentation/mapkit/mkplacemark/
You'll need to provide at least a set of location coordinates. Regarding the error message:
'init()' has been explicitly marked unavailable here (MapKit.MKPlacemark)
This is an Obj-C class, a subclass of NSObject
, so by default it inherits a parameter-less init()
. However, since MKPlacemark
doesn't support that, it's marked "unavailable" in the SDK to prevent you using it accidentally.