Skip to content

Instantly share code, notes, and snippets.

@nukka123
Created May 2, 2025 15:17
Show Gist options
  • Select an option

  • Save nukka123/e94c75748850bff2f0a39b8d468f5ba7 to your computer and use it in GitHub Desktop.

Select an option

Save nukka123/e94c75748850bff2f0a39b8d468f5ba7 to your computer and use it in GitHub Desktop.
swiftui-binding-bool
extension Binding {
func bool<T>() -> Binding<Bool> where Value == Optional<T> {
Binding<Bool> (
get: {
self.wrappedValue != nil
},
set: { newValue in
// alertはボタンタップで非表示に変化の時にfalseをsetする。
// その際には、元のOptional値をnilに更新する。
if !newValue {
self.wrappedValue = nil
}
// trueがsetの時は何もしない。
}
)
}
}
@nukka123
Copy link
Copy Markdown
Author

nukka123 commented May 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment