Created
July 5, 2024 10:34
-
-
Save pookjw/989076e8f4a531e8cb622255826de57b 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
import SwiftUI | |
@Observable | |
final class GoodColor: Identifiable { | |
var color: Color | |
var id: Color { | |
print(_color) | |
return color | |
} | |
init(color: Color) { | |
self.color = color | |
} | |
} | |
struct GoodView: View { | |
@State private var colors: [GoodColor] = [.init(color: .red), .init(color: .orange), .init(color: .yellow)] | |
var body: some View { | |
VStack { | |
ForEach(colors) { color in | |
color.color | |
.onTapGesture { | |
color.color = .green | |
} | |
} | |
} | |
} | |
} |
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
import SwiftUI | |
struct WrongColor: Identifiable, Equatable { | |
static func ==(lhs: WrongColor, rhs: WrongColor) -> Bool { | |
return lhs.color == rhs.color | |
} | |
var color: Color | |
var id: Color { | |
print(color) | |
return color | |
} | |
} | |
struct WrongView: View { | |
@State private var colors: [WrongColor] = [.init(color: .red), .init(color: .orange), .init(color: .yellow)] | |
var body: some View { | |
VStack { | |
ForEach(colors) { color in | |
color.color | |
.onTapGesture { | |
var colors: [WrongColor] = colors | |
var greenColor: WrongColor = color | |
greenColor.color = .green | |
let index: Int = colors.firstIndex(of: color)! | |
colors.remove(at: index) | |
colors.insert(greenColor, at: index) | |
self.colors = colors | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment