Tuesday, March 5, 2024
HomeiOS Developmentios - SwiftUI Picker does not work with array returned from perform

ios – SwiftUI Picker does not work with array returned from perform


I am attempting to get a picker to pick dimensions which I wish to return from a perform based mostly on a reputation. Once I return the array of allowed dimensions from a perform, the picker not works – it does not set the choice. It does work if I exploit a tough coded array of dimensions (which I actually do not wish to do). This is a fast instance (not the actual code) that exhibits my drawback.

import SwiftUI

struct ContentView: View
{
    static let Dimensions = [UnitLength.meters, UnitLength.yards, UnitLength.feet]
    
#if true
    let dimensions = Self.Dimensions
#else
    let dimensions = Self.getDimensions()
#endif
    
    @State var dimension = UnitLength.meters
    
    var physique: some View
    {
        Picker("Dimensions", choice: $dimension)
        {
            ForEach(dimensions, id: .self)
            {
                dimension in
                
                Textual content(dimension.image)
            }
        }
        .pickerStyle(MenuPickerStyle())
    }
    
    static func getDimensions() -> [Dimension]
    {
        Self.Dimensions
    }
}

When the #if on the high is true, the code works. When it’s false, it doesn’t work. Any thought why? Including a .tag(dimension) did not make any distinction.

I do get an error/warning within the debug console when #if is fake:

Picker: the choice “<_NSStatic_NSUnitLength: 0x1f58b3aa8> m” is
invalid and doesn’t have an related tag, it will give undefined
outcomes.

Printing dimensions in each circumstances seems to present the identical array.

I am utilizing Xcode 15.2 and testing with the simulator on iOS 17.2.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments