HomeiOS Developmentios - How do I get a Swift UI Picker to replace...

ios – How do I get a Swift UI Picker to replace to the chosen worth when pulling these values from a database?


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:

  1. Swift UI View that shows the Picker
  2. Knowledge Mannequin
  3. JSON Knowledge File

Beneath is a screenshot of the picker:

View of Picker Selection

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments