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()
}
}
Post
Replies
Boosts
Views
Activity
for sometime, preview is "collapsed", see attached snapshot
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.
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.