I have a @State property that's initialized with a struct I created (Landmark) that takes in a MKPlacemark.
@State private var selectedLandmark: Landmark = Landmark(placemark: MKPlacemark())
Here's the code for the struct:
import Foundation
import MapKit
struct Landmark {
let placemark: MKPlacemark
var id: UUID {
UUID()
}
var name: String {
placemark.name ?? ""
}
var title: String {
placemark.title ?? ""
}
var coordinate: CLLocationCoordinate2D {
placemark.coordinate
}
}
Whenever there are any updates to any state/binding vars within this view, I get a EXC_BAD_ACCESS error and no logs. If I remove the reference to MKPlacemark, everything works fine. Is this possibly a case of a strong reference being retained somewhere?
Steps to reproduce:
Create a "child" view:
import SwiftUI
import MapKit
struct MKPlaceChild: View {
@Binding var showCreateEvent: Bool
@State private var selectedLandmark: Landmark = Landmark(placemark: MKPlacemark())
var body: some View {
VStack {
Text("Hello, World!")
Button("Cancel") {
self.showCreateEvent = false
}
}
}
}
Create a parent view:
struct MKPlaceParent: View {
@State var showCreateEvent: Bool = true
var body: some View {
VStack {
if showCreateEvent {
MKPlaceChild(showCreateEvent: $showCreateEvent)
} else {
Text("Hello")
}
}
}
}
If you tap on "Cancel" button, an error occurs:( Any help would be appreciated
The problem is in wrong initialiser. Here is fixed part (tested with Xcode 12 / iOS 14)
struct MKPlaceChild: View {
@Binding var showCreateEvent: Bool
@State private var selectedLandmark: Landmark = Landmark(placemark:
MKPlacemark(coordinate: CLLocationCoordinate2D())) // << here !!
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.