Post

Replies

Boosts

Views

Activity

Reply to SwiftData model doesn't work with Transferable and Codable
Since @Model is a macro it would do something special (custom logic) for every member property to persist it. Workaround Create a struct ItemRecord with the same properties Populate ItemRecord with Item Conform ItemRecord to Codable and Transferable Let the Item have a way to conveniently create ItemRecord Use ItemRecord to drag and drop or for other transferable functionalities (item.record) Code @Model final class Item { let createdAt: Date //For convenience var record: ItemRecord { ItemRecord(item: self) } init() { self.createdAt = .now } } struct ItemRecord: Codable, Transferable { let createdAt: Date init(item: Item) { createdAt = item.createdAt } static var transferRepresentation: some TransferRepresentation { CodableRepresentation(contentType: .myCustomType) } }
Topic: Privacy & Security SubTopic: General Tags:
Aug ’23
Reply to dropDestination does not work inside List
@gchiste I have filed a feedback FB12980427 (contains sample code, videos) I really hope that the bug gets fixed, I have filed it under SwiftUI as CoreTransferable wasn't available in the frameworks list. Yes, this doesn't work on iOS but works on macOS. I have tested on iOS 17 beta using Xcode 15 beta 6.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to SwiftDataFlashCardSample doesn't compile
On Xcode 15 beta 6, following code compiles fine. The following code was taken from the Code tab of the https://developer.apple.com/wwdc23/10154 Why do you explicitly mention PersistentModel conformance when @Model already conforms to PersistentModel? Code (compiles fine) @Model final class Card { var front: String var back: String var creationDate: Date init(front: String, back: String, creationDate: Date = .now) { self.front = front self.back = back self.creationDate = creationDate } } Did you miss
Aug ’23
Reply to SwiftData @Query unable to filter on Int enum rawValue
Store the rawValue in a variable and access the variable inside the #Predicate closure. Given below is an example: enum AccountType: Int, Codable, CaseIterable { case creditAccountReceivable = 1, creditAccountPayable = 2, nominal = 3 } @Model class Account { var type: Int init(type: AccountType.RawValue) { self.type = type } } struct ContentView: View { @Query private var nominalAccounts: [Account] init() { //Store it in a variable type let type = AccountType.creditAccountPayable.rawValue let filter = #Predicate<Account> { account in //Access the variable inside the closure account.type == type } _nominalAccounts = Query(filter: filter, sort: \.type) } var body: some View { Text("Hello") } } Just a small suggestion, next time please post minimum code that would compile. Add the tag SwiftData to the post so that experts in SwiftData can view your question as your question is more related to SwiftData
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Programmatically create MenuBarExtra elements
MenuBarExtra vs Menu MenuBarExtra is like adding a mini app that lives on the menu bar What you are trying to achieve seems similar to what Menu does (https://developer.apple.com/documentation/swiftui/menu) Menu items could even have keyboard shortcuts. Pasted below is from Apple's documentation. Menu("Actions") { Button("Duplicate", action: duplicate) Button("Rename", action: rename) Button("Delete…", action: delete) Menu("Copy") { Button("Copy", action: copy) Button("Copy Formatted", action: copyFormatted) Button("Copy Library Path", action: copyPath) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Programmatically create MenuBarExtra elements
Use ForEach inside MenuBarExtra as shown below: @main struct SwiftBarApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } MenuBarExtra("Title") { ForEach(appDelegate.menuItems, id:\.self) { menuItemSet in MenuBarView(menuItemSet: menuItemSet) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Output print statements
Problem NavigationLink and Button both perform actions. So having both NavigationLink and Button doesn't make sense, so the action of the button is ignored. Points to note NavigationLink is used for navigation NavigationView is deprecated, so use NavigationStack instead Solution Given below is sample code using NavigationStack, use onChange(of:) to detect any changes to path. Tip You could even pass the path variable as a binding to the destination views struct ContentView: View { @State private var path = [Int]() var body: some View { NavigationStack(path: $path) { VStack { NavigationLink(value: 1) { Rectangle() .frame(width: 100, height: 100) } NavigationLink(value: 2) { Rectangle() .frame(width: 100, height: 100) } NavigationLink(value: 3) { Rectangle() .frame(width: 100, height: 100) } } .navigationDestination(for: Int.self) { value in Text("Selected box: \(value)") } } .onChange(of: path) { newPath in print("path: \(newPath)") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Xcode beta 5 crashes when adding a file to a project
Problem This happens consistently in the following environment: macOS: 14.0 Beta (23A5301h) Xcode: Version 15.0 beta 5 (15A5209g) Crash happens when adding a new file. Observation File gets written to disk then Xcode crashes. Subsequently after re-opening Xcode able to add a file from disk. However consisting crashes when creating a new file Works fine The same version of Xcode works fine on macOS 13.5
Aug ’23