I've never used Swift or SwiftUI before, so I went to the apple SwiftUI tutorials page to attempt to learn the language. In the SwiftUI tutorial: Creating and Combining Views, section 5, step 4 it asks me to insert the line Map(initialPosition: .region(region)
in the following code block
import SwiftUI
import MapKit
struct MapView: View {
var body: some View {
/** this line */
Map(initialPosition: .region(region))
}
private var region: MKCoordinateRegion {
MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),
span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
)
}
}
This code block is directly from the tutorial, however, I get an error stating Cannot infer contextual base in reference to member 'region'
. From my own research, there exists a function in MapKit called region
which converts from MKCoordinateRegion
to MapCameraRegion
, which seems to be the desired functionality as according to the documentation here initialPosition is supposed to be of type MapCameraRegion
. However, it seems I must be calling this function incorrectly. I thought a logical next step would be to insert the line Map(initialPosition: MapKit.region(region))
as that's how many other languages I've worked with operate but that gives the error Module 'MapKit' has no member named 'region'
. I also changed the name of my private var region
so the function and the variable wouldn't have the same name and that also had no affect. Any ideas?
Thanks in advance.