Post

Replies

Boosts

Views

Activity

Is SwiftUI Picker not compatible with a Bindable property?
I have a view with @Bindable property. (It's a model) I can use TextField("Name", text: $chore.name), but not Picker(selection: $chore.choreFrequency, label: EmptyView()) The auto-complete doesn't even show choreFrequency as an option enum ChoreFrequency: String, CaseIterable, Identifiable, Codable { case daily, weekly, monthly, seasonal var id: ChoreFrequency { self } } @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date } Can I not use the Bindable property in the Picker?
3
0
1.3k
Sep ’23
How do I filter @Query with bound item
I have a few that is passed an item of type FamilyMember. Available in the view is a list of Chores. A chore has a member assigned to it. I want to filter the chores to the passed family member. 2 Structures and view code: @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date init(id: UUID = UUID(), name: String = "", frequency: ChoreFrequency = .daily, assignedTo: FamilyMember = FamilyMember(), isComplete: Bool = false, dueDate: Date = Date()) { self.name = name self.frequency = frequency self.assignedTo = assignedTo self.isComplete = isComplete self.dueDate = dueDate } } @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name: String init(name: String = "Unassigned") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember @Query private var chores: [Chore] init(member: FamilyMember) { self.member = member let predicate = #Predicate<Chore> { $0.assignedTo == member // <— Error occurs here! } _chores = Query(filter: predicate, sort: \.dueDate) } . . . It's when creating the predicate that I'm running to an error of value conversion. Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Chore>, FamilyMember>, PredicateExpressions.Value<FamilyMember>>' to closure result type 'any StandardPredicateExpression<Bool>' What am I doing wrong?
2
0
1.3k
Sep ’23
How do I preview a View that's using @Bindable?
I have a SwiftUI view that is being passed a binding from a parent view. I would like to preview the subview, but can't. I tried .constant and a @Model object directly. Both ways crash the preview. Any idea on how I can get this to work properly? @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name = "" init(name: String = "") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember var body: some View { Text(member.name) } } #Preview { MemberView(member: FamilyMember(name: "Family member")) } #Preview { MemberView(member: .constant(FamilyMember(name: "Family member"))) }
3
1
3.5k
Sep ’23
largeTitleDisplayMode doesn't work when using UIHostingController
When using a UIHostingController to host my SwiftUI content, I can't get the navigation title to start large and shift to inline as the user scrolls. Is this a known problem with using the UIHostingController? var viewModel: DashboardViewModel! lazy var contentView = UIHostingController(rootView: DashboardView(viewModel: viewModel)) override func viewDidLoad() { super.viewDidLoad() addChild(contentView) view.addSubview(contentView.view) setupConstraints() navigationItem.title = "Test Title" navigationItem.largeTitleDisplayMode = .automatic } The title in the navigation controller shows inline
4
1
2k
Apr ’23
App installed outside of Xcode interrupts library code
I have a sample app here: https://github.com/AaronBratcher/MoneyTrak where, if I install it onto a device from Xcode it runs fine. (Shows a list of accounts) However if I archive and add the .ipa onto the device the database isn't initialized properly and I don't understand why. When the database is initially created in the AgileDB package, 2 tables should be created: __settings and __tableArrayColumns. If anyone can help, I would greatly appreciate it.
1
0
727
Jan ’23
DropInfo doesn't contain my drag object in SwiftUI
I have these extensions to my NSObject, Codable class: extension SavingsGoal: NSItemProviderWriting { public static let typeIdentifier = "com.AaronLBratcher.SavingsGoal.Drag" public static var writableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] } public typealias DragHandler = (Data?, Error?) -> Void public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping DragHandler) -> Progress? { do { print("^^^ encoding") let encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted completionHandler(try encoder.encode(self), nil) } catch { completionHandler(nil, error) } return nil } } extension SavingsGoal: NSItemProviderReading { public static var readableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] } public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> SavingsGoal { print("^^^ decoding") let decoder = JSONDecoder() return try decoder.decode(SavingsGoal.self, from: data) } } and these onDrag, onDrop method calls on my Views: .onDrag { NSItemProvider(object: goal) } .onDrop(of: [SavingsGoal.typeIdentifier], delegate: viewModel) Visually the drag works on the simulator, showing a plus when it hits the target View, however only the delegate's performDrop call is made. The loadData and object methods are not called in the extensions and the DropInfo doesn't contain my object. I have carefully checked and rechecked the code against available examples and cannot find what I did wrong.
1
0
655
Dec ’22
Why is rotationEffect moving vertically too?
I have my animation for the rotation working beautifully, but the image is also moving vertically. Why? struct SyncView: View { @State var isSyncing = false let animation: Animation = Animation.linear(duration: 2.0).repeatForever(autoreverses: false) var body: some View { VStack(spacing: 16) { HStack { VStack(alignment: .leading) { Text("Linked Device's Name") Text("Updating (Last update: Oct 1, 2022)") .font(.callout) .foregroundColor(.gray) } Spacer() Text(Image(systemName: "arrow.triangle.2.circlepath")) .foregroundStyle(Color.yellow) .font(.title) .rotationEffect(Angle.degrees(isSyncing ? 360 : 0)) .animation(animation, value: isSyncing) } } .padding() .onAppear { isSyncing = true } } }
1
0
893
Nov ’22
Xcode 14 NavigationPath – two views pushed onto nav stack when multiple links in form section
Create new SwiftUI project Replace ContentView with the code below Tap either link. Both views are pushed onto the nav stack class ContentViewModel: ObservableObject { @Published var navigationPath = NavigationPath() } struct ContentView: View { @StateObject var viewModel = ContentViewModel() var body: some View { NavigationStack(path: $viewModel.navigationPath) { Form { Section { VStack { NavigationLink(value: 1) { Text("Location") } NavigationLink(value: 2) { Text("Category") } } } }.navigationDestination(for: Int.self) { route in switch route { case 1: Text("Location") case 2: Text("Category") default: Text("Unknown") } }.navigationTitle("Home") } } } What am I doing wrong? I would only expect the tapped item to be pushed into the stack.
1
0
990
Oct ’22
Is there a cleaner way of making an enum with associated value conform to rawRepresentable?
I have this in my code and it works, however if I have a long list it gets tiresome. Is there a better way of having an enum with associated values that also conforms to RawRepresentable? public enum IconColor { case regular case error case warning case success case custom(String) public var value: Color { return loadColor(self.rawValue) } } extension IconColor: RawRepresentable { public var rawValue: String { switch self { case .regular: return "icon_regular" case .error: return "icon_error" case .warning: return "icon_warning" case .success: return "icon_success" case .custom(let value): return value } } public init(rawValue: String) { switch rawValue { case "icon_regular": self = .regular case "icon_error": self = .error case "icon_warning": self = .warning case "icon_success": self = .success default: self = .custom(rawValue) } } }
3
0
848
Sep ’22
SwiftUI image from Bundle.main not working
I have a project with a single asset image. In the dependency package, I am attempting to preview using the image in the project using Bundle.main. I just get a blank. How can I get this to work? Sample project here: https://github.com/AaronBratcher/SwiftUIPreviewProblem
Replies
6
Boosts
1
Views
1.2k
Activity
Oct ’24
Cannot Preview SwiftUI - dependent package
I have a normal UIKit project with a package dependency containing some SwiftUI. Opening the package on its own, I can preview the SwiftUI. However, when I have the package as a dependency, I get the dreaded message saying Active scheme does not build this file regardless of choosing the app or package scheme. Why can't I get a preview? Using Xcode 16
Replies
0
Boosts
0
Views
368
Activity
Sep ’24
Xcode 16 preview not working
I want to add SwiftUI to an existing package. Super simple to get started. It previews with Xcode 15.4, but gives an error on Xcode 16.0: JITError: Runtime linking failure Anyone have an idea on what may be failing? Full SwiftUI code: import SwiftUI struct Dashboard: View { var body: some View { Text("hello") } } #Preview { Dashboard() }
Replies
4
Boosts
3
Views
3.8k
Activity
Sep ’24
How to override [CodingKey] to an Encoder or Decoder?
The Encoder and Decoder protocols have this property: var codingPath: [CodingKey] { get } In my own custom coding class, how do I override this and pass valid CodingKeys? Every document and code example uses the enum to define coding keys. Thanks.
Replies
1
Boosts
0
Views
517
Activity
Oct ’23
Is SwiftUI Picker not compatible with a Bindable property?
I have a view with @Bindable property. (It's a model) I can use TextField("Name", text: $chore.name), but not Picker(selection: $chore.choreFrequency, label: EmptyView()) The auto-complete doesn't even show choreFrequency as an option enum ChoreFrequency: String, CaseIterable, Identifiable, Codable { case daily, weekly, monthly, seasonal var id: ChoreFrequency { self } } @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date } Can I not use the Bindable property in the Picker?
Replies
3
Boosts
0
Views
1.3k
Activity
Sep ’23
How do I filter @Query with bound item
I have a few that is passed an item of type FamilyMember. Available in the view is a list of Chores. A chore has a member assigned to it. I want to filter the chores to the passed family member. 2 Structures and view code: @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date init(id: UUID = UUID(), name: String = "", frequency: ChoreFrequency = .daily, assignedTo: FamilyMember = FamilyMember(), isComplete: Bool = false, dueDate: Date = Date()) { self.name = name self.frequency = frequency self.assignedTo = assignedTo self.isComplete = isComplete self.dueDate = dueDate } } @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name: String init(name: String = "Unassigned") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember @Query private var chores: [Chore] init(member: FamilyMember) { self.member = member let predicate = #Predicate<Chore> { $0.assignedTo == member // <— Error occurs here! } _chores = Query(filter: predicate, sort: \.dueDate) } . . . It's when creating the predicate that I'm running to an error of value conversion. Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Chore>, FamilyMember>, PredicateExpressions.Value<FamilyMember>>' to closure result type 'any StandardPredicateExpression<Bool>' What am I doing wrong?
Replies
2
Boosts
0
Views
1.3k
Activity
Sep ’23
How do I preview a View that's using @Bindable?
I have a SwiftUI view that is being passed a binding from a parent view. I would like to preview the subview, but can't. I tried .constant and a @Model object directly. Both ways crash the preview. Any idea on how I can get this to work properly? @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name = "" init(name: String = "") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember var body: some View { Text(member.name) } } #Preview { MemberView(member: FamilyMember(name: "Family member")) } #Preview { MemberView(member: .constant(FamilyMember(name: "Family member"))) }
Replies
3
Boosts
1
Views
3.5k
Activity
Sep ’23
Swift Data in Background Processing?
Is it possible to do Swift Data operations on a background processing event?
Replies
0
Boosts
2
Views
503
Activity
Aug ’23
SwiftData Aggregation
Given a simple class: final class Person { name: String age: Int } How can I get aggregated data such as average age, or count of people within specific ranges without retrieving all the data and doing the aggregates myself?
Replies
2
Boosts
0
Views
1.4k
Activity
Aug ’23
largeTitleDisplayMode doesn't work when using UIHostingController
When using a UIHostingController to host my SwiftUI content, I can't get the navigation title to start large and shift to inline as the user scrolls. Is this a known problem with using the UIHostingController? var viewModel: DashboardViewModel! lazy var contentView = UIHostingController(rootView: DashboardView(viewModel: viewModel)) override func viewDidLoad() { super.viewDidLoad() addChild(contentView) view.addSubview(contentView.view) setupConstraints() navigationItem.title = "Test Title" navigationItem.largeTitleDisplayMode = .automatic } The title in the navigation controller shows inline
Replies
4
Boosts
1
Views
2k
Activity
Apr ’23
App installed outside of Xcode interrupts library code
I have a sample app here: https://github.com/AaronBratcher/MoneyTrak where, if I install it onto a device from Xcode it runs fine. (Shows a list of accounts) However if I archive and add the .ipa onto the device the database isn't initialized properly and I don't understand why. When the database is initially created in the AgileDB package, 2 tables should be created: __settings and __tableArrayColumns. If anyone can help, I would greatly appreciate it.
Replies
1
Boosts
0
Views
727
Activity
Jan ’23
DropInfo doesn't contain my drag object in SwiftUI
I have these extensions to my NSObject, Codable class: extension SavingsGoal: NSItemProviderWriting { public static let typeIdentifier = "com.AaronLBratcher.SavingsGoal.Drag" public static var writableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] } public typealias DragHandler = (Data?, Error?) -> Void public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping DragHandler) -> Progress? { do { print("^^^ encoding") let encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted completionHandler(try encoder.encode(self), nil) } catch { completionHandler(nil, error) } return nil } } extension SavingsGoal: NSItemProviderReading { public static var readableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] } public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> SavingsGoal { print("^^^ decoding") let decoder = JSONDecoder() return try decoder.decode(SavingsGoal.self, from: data) } } and these onDrag, onDrop method calls on my Views: .onDrag { NSItemProvider(object: goal) } .onDrop(of: [SavingsGoal.typeIdentifier], delegate: viewModel) Visually the drag works on the simulator, showing a plus when it hits the target View, however only the delegate's performDrop call is made. The loadData and object methods are not called in the extensions and the DropInfo doesn't contain my object. I have carefully checked and rechecked the code against available examples and cannot find what I did wrong.
Replies
1
Boosts
0
Views
655
Activity
Dec ’22
Why is rotationEffect moving vertically too?
I have my animation for the rotation working beautifully, but the image is also moving vertically. Why? struct SyncView: View { @State var isSyncing = false let animation: Animation = Animation.linear(duration: 2.0).repeatForever(autoreverses: false) var body: some View { VStack(spacing: 16) { HStack { VStack(alignment: .leading) { Text("Linked Device's Name") Text("Updating (Last update: Oct 1, 2022)") .font(.callout) .foregroundColor(.gray) } Spacer() Text(Image(systemName: "arrow.triangle.2.circlepath")) .foregroundStyle(Color.yellow) .font(.title) .rotationEffect(Angle.degrees(isSyncing ? 360 : 0)) .animation(animation, value: isSyncing) } } .padding() .onAppear { isSyncing = true } } }
Replies
1
Boosts
0
Views
893
Activity
Nov ’22
Xcode 14 NavigationPath – two views pushed onto nav stack when multiple links in form section
Create new SwiftUI project Replace ContentView with the code below Tap either link. Both views are pushed onto the nav stack class ContentViewModel: ObservableObject { @Published var navigationPath = NavigationPath() } struct ContentView: View { @StateObject var viewModel = ContentViewModel() var body: some View { NavigationStack(path: $viewModel.navigationPath) { Form { Section { VStack { NavigationLink(value: 1) { Text("Location") } NavigationLink(value: 2) { Text("Category") } } } }.navigationDestination(for: Int.self) { route in switch route { case 1: Text("Location") case 2: Text("Category") default: Text("Unknown") } }.navigationTitle("Home") } } } What am I doing wrong? I would only expect the tapped item to be pushed into the stack.
Replies
1
Boosts
0
Views
990
Activity
Oct ’22
Is there a cleaner way of making an enum with associated value conform to rawRepresentable?
I have this in my code and it works, however if I have a long list it gets tiresome. Is there a better way of having an enum with associated values that also conforms to RawRepresentable? public enum IconColor { case regular case error case warning case success case custom(String) public var value: Color { return loadColor(self.rawValue) } } extension IconColor: RawRepresentable { public var rawValue: String { switch self { case .regular: return "icon_regular" case .error: return "icon_error" case .warning: return "icon_warning" case .success: return "icon_success" case .custom(let value): return value } } public init(rawValue: String) { switch rawValue { case "icon_regular": self = .regular case "icon_error": self = .error case "icon_warning": self = .warning case "icon_success": self = .success default: self = .custom(rawValue) } } }
Replies
3
Boosts
0
Views
848
Activity
Sep ’22