Home > Back-end >  SwiftUI Picker with Int16
SwiftUI Picker with Int16

Time:01-05

Inside core data I'm using Int16, but using picker with Int16 doesn't work. Does anyone know why? The work around I'm using is to create a model to manage values in Int, during the saving, I transform values in Int16.

struct PikHeiVIEW: View {
    @Binding var height:Int
    @Binding var heightInt16:Int16
    
    var body: some View {
        VStack {
            Picker("", selection: $height) {
                ForEach(150...190, id: \.self) {
                    Text("\($0)")
                }
            }
            Text("You selected: \(height)")
            Picker("", selection: $heightInt16) {
                ForEach(150...190, id: \.self) {
                    Text("\($0)")
                }
            }
            Text("You selected: \(heightInt16)")
        }
    }
}

struct TESTVIEW: View {
        
    @State private var height:Int = 180
    @State private var heightInt16:Int16 = 180
    
    var body: some View {
        PikHeiVIEW(height: $height, heightInt16: $heightInt16)
    }
}
struct TESTVIEW_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            TESTVIEW()
        }
    }
}

CodePudding user response:

You can create your picker range using Int16

Picker("", selection: $heightInt16) {
    ForEach(Int16(150)...Int16(190), id: \.self) {
        Text("\($0)")
    }
}
  • Related