Drawback
My Picker will not replace once I choose a brand new worth.
After I click on/faucet one other worth, the picker closes and shows “Austin”, it doesn’t matter what I select.
I’m utilizing:
Xcode Model 14.2
I’ve supplied three items of code under:
- Swift UI View that shows the Picker
- Knowledge Mannequin
- JSON Knowledge File
Beneath is a screenshot of the picker:
Right here is the code with the Picker:
import SwiftUI
struct AddEditExpenseView: View {
@EnvironmentObject var destinationsModelData: DestinationsModelData
var destinationIndex: Int {
destinationsModelData.locations.firstIndex(the place: { $0.id == vacation spot.id })!
}
var vacation spot: Vacation spot
@State var selectedDestination: String = ""
@State var saveDestinationFieldTextArray: [String] = []
var physique: some View {
NavigationView {
Type {
Part(header: Textual content("Vacation spot")) {
Picker("Vacation spot", choice: $selectedDestination) {
ForEach(destinationsModelData.locations) { vacation spot in
Textual content(vacation spot.identify)
}
}
.onAppear {
selectedDestination = vacation spot.identify
}
.foregroundColor(.black)
}
}
.formStyle(GroupedFormStyle())
.accentColor(.black)
.navigationBarTitle(Textual content("Add, Edit Expense"))
.navigationBarBackButtonHidden(true)
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct AddEditExpenseView_Previews: PreviewProvider {
static let destinationsModelData = DestinationsModelData()
static var previews: some View {
AddEditExpenseView(vacation spot: destinationsModelData.locations[0])
.environmentObject(destinationsModelData)
}
}
Right here is the Vacation spot Mannequin:
import Basis
import SwiftUI
import CoreLocation
struct Vacation spot: Hashable, Codable, Identifiable {
var id: Int
var identify: String
var metropolis: String
var state: String
var nation: String
var description: String
var isOpen: Bool
var isCompared: Bool
personal var imageName: String
var picture: Picture {
Picture(imageName)
}
personal var coordinates: Coordinates
var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D (
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}
struct Coordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
}
I’ve a JSON file that shops the info:
[
{
"name": "Austin",
"category": "Cities",
"city": "Austin",
"state": "Texas",
"country": "USA",
"id": 1001,
"isOpen": true,
"isCompared": true,
"coordinates": {
"longitude": -97.743057,
"latitude": 30.267153
},
"description": "placeholder text",
"imageName": "Austin_TX"
},
{destination2},
{destination3}
]
I attempted to make use of a @FetchRequest
however I get an error message that @FetchRequest
is just not supported.
I attempted .onChange
with no luck. I could not get to the compiling stage right here.