onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
669 stars 33 forks source link

How to use Picker with optional selection in SwiftUI #849

Open onmyway133 opened 2 years ago

onmyway133 commented 2 years ago

We need to explicitly specify optional in tag

extension AVCaptureDevice: Identifiable {
    public var id: String { uniqueID }
}

@State var device: AVCaptureDevice?

Picker("Camera", selection: $device) {
    ForEach(manager.devices) { d in
        Text(d.localizedName)
            .tag(AVCaptureDevice?.some(d))
    }
}