Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created July 5, 2024 10:34
Show Gist options
  • Save pookjw/989076e8f4a531e8cb622255826de57b to your computer and use it in GitHub Desktop.
Save pookjw/989076e8f4a531e8cb622255826de57b to your computer and use it in GitHub Desktop.
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
}
}
}
}
}
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