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() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
6
1
1.8k
Aug ’24
@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
917
Nov ’23
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() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
6
Boosts
1
Views
1.8k
Activity
Aug ’24
Xcode 16.0 - sometimes preview "collapsed"
for sometime, preview is "collapsed", see attached snapshot
Replies
3
Boosts
0
Views
579
Activity
Jul ’24
@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.
Replies
3
Boosts
3
Views
917
Activity
Nov ’23
Xcode 15 beta 2 - SampleTrip preview does not compile
I got the errors like Type 'Trip' has no member 'preview', but the preview is defined in Trip.swift comment all preview code with errors and the project can be compiled.
Replies
2
Boosts
0
Views
1.1k
Activity
Jun ’23