Post

Replies

Boosts

Views

Activity

iOS 17 - TextField color does not changes with state is updated
Hi! I find this code does not work as expected on iOS 17 simulator and device. It was working correctly with iOS 16: struct ContentView: View { @State private var numberText = "" var color: Color { let result = (Int(numberText) ?? 0) <= 0 if result { return .black } return .red } var body: some View { VStack { TextField("Enter a number", text: $numberText) .font(.title) .foregroundStyle( color ) Text("Font color will turn red if number > 0") .foregroundColor(.gray) } .padding() } } I tried a workaround and it works: struct ContentView: View { @State private var numberText = "" @State private var color = Color.black func updateColor() ->Color { let result = (Int(numberText) ?? 0) <= 0 if result { return .black } return .red } var body: some View { VStack { TextField("Enter a number", text: $numberText) .font(.title) .foregroundStyle( color ) .onChange(of:numberText) { color = updateColor() } Text("Font color will turn red if number > 0") .foregroundColor(.gray) } .padding() } }
6
1
1.3k
Oct ’23
@Model and Encodable cause the error Ambiguous use of 'setValue(for:to:)'
I test with a simple class and it can compile: @Model final class Book: Encodable { var name: String enum CodingKeys: CodingKey { case name } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(name, forKey: .name) } } Then I try with the Trip class from sample and it cause an error: @Model final class Trip: Encodable { : enum CodingKeys: CodingKey { case name, destination, endDate, startDate, bucketList } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(name, forKey: .name) try container.encode(destination, forKey: .destination) try container.encode(endDate, forKey: .endDate) try container.encode(startDate, forKey: .startDate) try container.encode(bucketList as! Set<BucketListItem>, forKey: .bucketList) } And I got the error Ambiguous use of 'setValue(for:to:)' I tried make BucketListItem and LivingAccommodation Encodable and still have error.
3
3
703
Jun ’23