Post

Replies

Boosts

Views

Activity

Reply to Setting multiple alignment guides in SwiftUI behaves strangely
You change center before bottom .alignmentGuide(VerticalAlignment.center) { dim in dim[.top] // re-assign center to 0 } .alignmentGuide(VerticalAlignment.top) { dim in dim[.bottom] } .alignmentGuide(VerticalAlignment.bottom) { dim in dim[VerticalAlignment.center] // 0 } In order to avoid mutual dependence, center value can not be used when calculating bottom. .alignmentGuide(VerticalAlignment.bottom) { dim in dim.height / 2 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’24
Reply to Lazy init of @State objects using new Observable protocol
try this: @MainActor @propertyWrapper public struct LazyState<T: Observable>: @preconcurrency DynamicProperty { @State private var holder: Holder public var wrappedValue: T { holder.wrappedValue } public var projectedValue: Binding<T> { return Binding(get: { wrappedValue }, set: { _ in }) } public func update() { guard !holder.onAppear else { return } holder.setup() } public init(wrappedValue thunk: @autoclosure @escaping () -> T) { _holder = State(wrappedValue: Holder(wrappedValue: thunk())) } } extension LazyState { final class Holder { private var object: T! private let thunk: () -> T var onAppear = false var wrappedValue: T { object } func setup() { object = thunk() onAppear = true } init(wrappedValue thunk: @autoclosure @escaping () -> T) { self.thunk = thunk } } } // Demo struct LinkViewUsingLazyState: View { @LazyState var object = TestObservationObject() var body: some View { Text("LazyState") Text(object.name) @Bindable var o = object TextField("hello", text: $o.name) Button("change") { object.name = "\(Int.random(in: 0 ... 1000))" } } } @Observable class TestObservationObject { init() { print("init") } var name = "abc" }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’25
Reply to What is going on with transformable
@Jerome_Donfack There are two main requirements to use transformer in SwiftData: The type returned by transformedValueClass needs to correspond to the type you declare in the model, so it needs to be changed to NSArray.self transformedValue can only be encoded as NSData, and in CoreData, we can encode into any supported type, such as NSNumber, NSString Override class func transformedValueClass() -> AnyClass { Return NSArray.self // change to NSArray } BTW: If it is not to be compatible with your other custom data, SwiftData will use the built-in transaformer to encode [String] without using transformable
Jul ’25