yes; I did get that error and changed the initial value to 1;
when you ran on mac mini did it work properly (like my desktop) or not (like MacBook pro)?
I made a few other changes to the code also (to display the hardware name, etc); updated code for Content View: (not sure how to edit original post)
enum FFocus: Hashable {
case pkNames
case btnAssign
case tfValue
case btn1
case btn2
case noFocus
}
struct PZParm: Identifiable {
var id = 0
var name = ""
}
struct ContentView: View {
@State var psel:Int = 0
@State var tfVal = "Testing"
@State var hw = ""
@FocusState var hwFocus:FFocus?
var body: some View {
VStack {
Text("Hardware test on " + hw).font(.title2)
PPDefView(bSel: $psel)
.focused($hwFocus, equals: .pkNames)
TextField("testing", text:$tfVal)
.frame(width: 400)
.focused($hwFocus, equals: .tfValue)
HStack {
Button("Button1", action: {})
.frame(width: 150)
.focused($hwFocus, equals: .btn1)
Button("Button2", action: {
tfVal = ""
hwFocus = .tfValue
})
.frame(width: 150)
.focused($hwFocus, equals: .btn2)
Button("New", action: {
tfVal = ""
psel = 0
hwFocus = .pkNames
})
.frame(width: 150)
.focused($hwFocus, equals: .btnAssign)
}
}
.padding()
// handle picker change
.onChange(of: psel, {
if psel > 0 {hwFocus = .tfValue}
})
.onAppear(perform: {
hw = hwn()
hwFocus = .pkNames}
)
}
// generate hardware model name
// using sysctlbyname
//
func hwn() -> String {
var len = 0
sysctlbyname("hw.model", nil, &len, nil, 0)
if len > 0 {
var rawName = Array<UInt8>(repeating: 0, count: len)
sysctlbyname("hw.model", &rawName, &len, nil, 0)
if let s = String(bytes: rawName, encoding: .utf8) {
return s
}
}
return "Unknown"
}
}
#Preview {
ContentView()
}
struct PPDefView: View {
@Binding var bSel:Int
// test defs
let pzparms:[PZParm] = [
PZParm.init(id:1, name:"Name1"),
PZParm.init(id:2, name:"Name2"),
PZParm.init(id:3, name:"Name3")
]
//
var body:some View {
Picker(selection: $bSel, label: Text("Name")) {
if bSel == 0 {
Text("no selection").tag(0)
}
ForEach(pzparms) {Text($0.name)}
}.frame(width: 250)
}
}