Created
June 21, 2019 08:43
-
-
Save alirp88/613c31d5fa372a5d565f4605dfe5e497 to your computer and use it in GitHub Desktop.
This file contains 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
struct FavoriteNumber { | |
var number: Int | |
var isFavorite: Bool | |
init(_ number: Int) { | |
self.number = number | |
isFavorite = false | |
} | |
} | |
struct ContentView : View { | |
@State var numbers = (1...10).map(FavoriteNumber.init) | |
var body: some View { | |
NavigationView { | |
VStack { | |
List($numbers.identified(by: \.value.number)) { num in | |
Toggle(isOn: num.isFavorite) { | |
Text("I like the number \(num.value.number)") | |
} | |
} | |
Text("The sum of my favorite numbers is \(sum)") | |
.padding() | |
} | |
.navigationBarTitle(Text("My favorite numbers!")) | |
} | |
} | |
var sum: Int { numbers.filter { $0.isFavorite }.reduce(0) { $0 + $1.number } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment