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
Reply to Using if statement in .overlay causes app to freeze when using withAnimation
func hide() { title = "" message = "" percentage = 0 withAnimation { isShowing = false } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’24
Reply to Is there any plans to isolate alignmentGuide's computeValue closure?
ZStack(alignment: .bottom) { ..... let isVisible = isVisible // Remove isolation bottomSheetView .alignmentGuide(VerticalAlignment.bottom) { dimension in isVisible ? dimension[.bottom] : dimension[.top] } } or .alignmentGuide(VerticalAlignment.bottom) { dimension in _isVisible.wrappedValue ? dimension[.bottom] : dimension[.top] }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’24
Reply to Strange crash when using .values from @Published publisher
let publisher = $count .receive(on: DispatchQueue.main) // receive in the main thread .map { String(describing: $0) } .removeDuplicates()
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’24
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:
Replies
Boosts
Views
Activity
Dec ’24
Reply to How often iCloud will sync with local swiftData?
You need deploy schema to production environment in cloudKit dashboard. more detail https://fatbobman.com/en/snippet/why-core-data-or-swiftdata-cloud-sync-stops-working-after-app-store-login/
Replies
Boosts
Views
Activity
Mar ’25
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:
Replies
Boosts
Views
Activity
Mar ’25
Reply to Selecting TextField Causes Application Hang on Device
Do not use 'dismiss'.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Mar ’25
Reply to Markdown openURL can not handle property url
It should be a bug, maybe try this var str: AttributedString { try! AttributedString(markdown: "[Terms of Service](\(url))") } Text(str1)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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
Replies
Boosts
Views
Activity
Jul ’25