Created
May 2, 2025 15:17
-
-
Save nukka123/e94c75748850bff2f0a39b8d468f5ba7 to your computer and use it in GitHub Desktop.
swiftui-binding-bool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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の時は何もしない。 | |
| } | |
| ) | |
| } | |
| } |
Author
nukka123
commented
May 2, 2025
- Seealso: https://qiita.com/nukka123/items/c7d5eb53b66ef9958198
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment