我是 iOS 新手,现在正尝试在 swiftui 视图中实现我自己的 init 方法。Init 有一个参数,必须为两个 State 赋值。但是,无论我做什么,它都不能正确填充它们。我使用 MapKit 和 CLLocationCoordinate2D 进行地理定位。只是补充一下,坐标: init 中的 CLLocationCoordinate2D 具有正确的值,但状态没有。我在这里实现的视图是 ModalView 知道该怎么做吗?这是我的代码。
@State var coordinate = CLLocationCoordinate2D()
@State private var name: String = ""
@State private var price: Decimal = 0
@State private var priceStr: String = ""
@State private var showAlert = false
@State private var location = ParkPlaceRespons(id: -1, name: "", price: -0, longitude: 18.42645, latitude: 43.85623)
@State private var mapArray = [ParkPlaceRespons]()
var alert: Alert{
Alert(title: Text("Error"), message: Text("Your price input is not in right format"), dismissButton: .default(Text("Ok")))}
init(coordinates: CLLocationCoordinate2D){
print(coordinates) // CLLocationCoordinate2D(latitude: 43.85613, longitude: 18.42745)
self.coordinate = coordinates
print(self.coordinate) // CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
self.coordinate.latitude = coordinates.latitude
self.coordinate.longitude = coordinates.longitude
self.location.longitude = coordinates.longitude
self.location.latitude = coordinates.latitude
print(location) // Does not work as well
self.mapArray.append(location)
print(mapArray) // Empty array
}