[Code-along] SwiftUI on the Mac: Build the fundamentals

RSS for tag

Discuss the WWDC21 session [Code-along] SwiftUI on the Mac: Build the fundamentals.

Posts under wwdc21-10062 tag

9 Posts

Post

Replies

Boosts

Views

Activity

How to declare a TableColumn with nullable field?
How does one declare a TableColumn with a nullable field? I have a Book model with several nullable fields: struct Book: Codable, Equatable, Identifiable { // ... let productURL: String? // ... } This is how I'm trying define the corresponding TableColumn: TableColumn("Product URL", value: \.productURL) { book in Text(String(book.productURL ?? "")) } Though this results in several errors: Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject' Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject' Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent Other, non-nullable columns work just fine. For example: TableColumn("ID", value: \.id) { book in     Text(String(book.id)) } TableColumn("Slug", value: \.slug) TableColumn("Category", value: \.category) TableColumn("Title", value: \.title) // ...
8
0
3.6k
Nov ’23
@SceneStorage not working on macOS
Hi, using the following ContentView in a SwiftUI app on macOS I would expect that the state of the toggle persists across application launches: struct ContentView: View { @SceneStorage("Toggle") var onOrOff: Bool = false var body: some View { VStack { Toggle("Will it persist?", isOn: $onOrOff) .padding() } } } To my surprise it does not persist. Am I wrong about how @SceneStorage should work? (I am trying this on the lates macOS/Xcode versions) Does @SceneStorage work for anybody on macOS? Thanks for your feedback! Cheers, Michael
4
0
2.6k
Sep ’23
Table view with dynamic content - is it possible?
Hi, How can I use a Table view to build a data grid for a dynamic set of data? Say, I have a 2D collection of data elements (a matrix, basically) with variable number of columns, and a 1D collection of column labels (a vector). For the sake of simplicity, let's assume that all data elements are Strings. let data: [[String]]; let columnLabels: [String] How can I build TableColumn definitions without key paths? Thanks!
6
0
3.0k
May ’22
Loading Error in Landmarks tutorial
I am working through the "Creating and Combining Views" Swift UI Landmarks tutorial using MacOS 12.3 and XCode 13.3. When trying to preview the MapView I receive the error: |  LoadingError: failed to load library at path "/Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib": Optional(dlopen(/Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib, 0x0002): Symbol not found: _$s9Landmarks16MapView_PreviewsV8previewsQrvgZTx |    Referenced from: /Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib |    Expected in: /Users/Elizabeth_Russell/Library/Developer/Xcode/UserData/Previews/Simulator Devices/49F2A163-EF87-47EC-B1F3-425A9742D1F7/data/Containers/Bundle/Application/0F7E6CF4-2589-42E5-B1C5-EE37941E4186/Landmarks.app/Landmarks) Can someone help me understand why the library is not being loaded? Thank you
1
0
765
Apr ’22
How to implement the right detail panel by swiftUI?
The video wwdc21-10062 claimed that by using swiftUI developers can build an app with a detail panel (like the right sidebar in Xcode), but didn't say how to implement that. I have tried to use HSplitView but failed. My code is like: var body: some View { NavigationView { Text("sidebar") HSplitView { Text("Primary View") Text("Detail Panel") } } }
3
0
2.2k
Feb ’22
Preview fails in part1 - can't follow along
This problem was posted previously but the answer was to update MacOS and Xcode. I am using the latest versions and the problem persists - for me at least. This is probably something really simple that everyone does and I just don't know about. I installed MacOS Monterey, XCode and downloaded the Building a Great Mac App with SwiftUI sample code so I can code along. After opening the workspace I added a developer account and added an Apple Development certificate. I think I should be able to build code now. I configure my developer team (for the target) to the development account I just setup, and the signing certificate to "Development". 2 warnings appear before I do anything: asking me to enable hardened runtime. Since it recommends I should do this, I tell it it's ok to go ahead and configure this. This gets rid of all the warnings for the moment. I open the ContentView (Part1 start -> GardenApp -> Garden App -> Navigation -> ContentView). An error shows instead of a preview: "Cannot preview the file - active scheme does not build this file". There's an information icon next to the error that reveals: "Select a scheme that builds a target which contains the current file, or add this file to a target that is built by the current scheme." When I search for "scheme" in the build settings I get nothing. This seems pretty odd. Any suggestions on how to resolve this would be great.
1
0
1.9k
Dec ’21
Beach ball and Memory Leak after completing code for Garden App Start when clicking "+" Button in Toolbar.
Hi, has anyone experienced the same problem? Also, while the New Plant menu command seems to work at least one time, the WaterPlants Menu command creates the same crash... Debug instrumentation shows a race condition in memory consumption, and the app needs to be force quit or the debug process terminated in Xcode. XCode Version is: 13.2 (13C90) MacOS Version is: 12.1 MacOS Build Target is: 12.0 Machine is: MacBook Air M1 2020, 8GB RAM, 256 GB SSD.
0
0
587
Dec ’21
Table() structure selection takes Object.ID but how to obtain reference to NSManagedObject from this ID
I am using the new SwiftUI Table() structure available for macOS. This structure takes a selection of multiple rows as a Set<Object.ID> and can be stored in a state property wrapper like so... @State private var selectedPeople = Set<Person.ID>() (per the example in the Apple documentation) Table() doesn't take any other type of identifier (that I am aware of), so I am constrained to use the ObjectIdentifier unique identifier. I am using Table() to present a list of NSManagedObjects. These NSManagedObjects are sourced from a Core Data Entity relationship property, in this case via a @FetchRequest. I want to be able to add new instances of the entity and also delete existing instances of the entity. I have worked out how to add a new instance - this is relatively easy. What I am struggling to work out is how to delete an NSManagedObject from the Table() by using the Object.ID that is used to track row selection. I have attempted this...     @State private var tableSelection = Set<Action.ID>() ... for actionID in tableSelection { let context = event.managedObjectContext let actionToDelete = context?.object(with: actionID) print(actionToDelete) context?.delete(actionToDelete) } but of course I have a type mismatch here and the compiler complains... Cannot convert value of type 'Action.ID' (aka 'ObjectIdentifier') to expected argument type 'NSManagedObjectID' So how I do grab a reference to the selected NSManagedObject from the set of Action.ID?
1
0
1.7k
Dec ’21
How to declare a TableColumn with nullable field?
How does one declare a TableColumn with a nullable field? I have a Book model with several nullable fields: struct Book: Codable, Equatable, Identifiable { // ... let productURL: String? // ... } This is how I'm trying define the corresponding TableColumn: TableColumn("Product URL", value: \.productURL) { book in Text(String(book.productURL ?? "")) } Though this results in several errors: Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject' Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject' Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent Other, non-nullable columns work just fine. For example: TableColumn("ID", value: \.id) { book in     Text(String(book.id)) } TableColumn("Slug", value: \.slug) TableColumn("Category", value: \.category) TableColumn("Title", value: \.title) // ...
Replies
8
Boosts
0
Views
3.6k
Activity
Nov ’23
@SceneStorage not working on macOS
Hi, using the following ContentView in a SwiftUI app on macOS I would expect that the state of the toggle persists across application launches: struct ContentView: View { @SceneStorage("Toggle") var onOrOff: Bool = false var body: some View { VStack { Toggle("Will it persist?", isOn: $onOrOff) .padding() } } } To my surprise it does not persist. Am I wrong about how @SceneStorage should work? (I am trying this on the lates macOS/Xcode versions) Does @SceneStorage work for anybody on macOS? Thanks for your feedback! Cheers, Michael
Replies
4
Boosts
0
Views
2.6k
Activity
Sep ’23
Table view with dynamic content - is it possible?
Hi, How can I use a Table view to build a data grid for a dynamic set of data? Say, I have a 2D collection of data elements (a matrix, basically) with variable number of columns, and a 1D collection of column labels (a vector). For the sake of simplicity, let's assume that all data elements are Strings. let data: [[String]]; let columnLabels: [String] How can I build TableColumn definitions without key paths? Thanks!
Replies
6
Boosts
0
Views
3.0k
Activity
May ’22
Loading Error in Landmarks tutorial
I am working through the "Creating and Combining Views" Swift UI Landmarks tutorial using MacOS 12.3 and XCode 13.3. When trying to preview the MapView I receive the error: |  LoadingError: failed to load library at path "/Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib": Optional(dlopen(/Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib, 0x0002): Symbol not found: _$s9Landmarks16MapView_PreviewsV8previewsQrvgZTx |    Referenced from: /Users/Elizabeth_Russell/Library/Developer/Xcode/DerivedData/Landmarks-fifnagwlpuhontdywqyzptoyghbg/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/arm64/MapView.1.preview-thunk.dylib |    Expected in: /Users/Elizabeth_Russell/Library/Developer/Xcode/UserData/Previews/Simulator Devices/49F2A163-EF87-47EC-B1F3-425A9742D1F7/data/Containers/Bundle/Application/0F7E6CF4-2589-42E5-B1C5-EE37941E4186/Landmarks.app/Landmarks) Can someone help me understand why the library is not being loaded? Thank you
Replies
1
Boosts
0
Views
765
Activity
Apr ’22
How to implement the right detail panel by swiftUI?
The video wwdc21-10062 claimed that by using swiftUI developers can build an app with a detail panel (like the right sidebar in Xcode), but didn't say how to implement that. I have tried to use HSplitView but failed. My code is like: var body: some View { NavigationView { Text("sidebar") HSplitView { Text("Primary View") Text("Detail Panel") } } }
Replies
3
Boosts
0
Views
2.2k
Activity
Feb ’22
Preview fails in part1 - can't follow along
This problem was posted previously but the answer was to update MacOS and Xcode. I am using the latest versions and the problem persists - for me at least. This is probably something really simple that everyone does and I just don't know about. I installed MacOS Monterey, XCode and downloaded the Building a Great Mac App with SwiftUI sample code so I can code along. After opening the workspace I added a developer account and added an Apple Development certificate. I think I should be able to build code now. I configure my developer team (for the target) to the development account I just setup, and the signing certificate to "Development". 2 warnings appear before I do anything: asking me to enable hardened runtime. Since it recommends I should do this, I tell it it's ok to go ahead and configure this. This gets rid of all the warnings for the moment. I open the ContentView (Part1 start -> GardenApp -> Garden App -> Navigation -> ContentView). An error shows instead of a preview: "Cannot preview the file - active scheme does not build this file". There's an information icon next to the error that reveals: "Select a scheme that builds a target which contains the current file, or add this file to a target that is built by the current scheme." When I search for "scheme" in the build settings I get nothing. This seems pretty odd. Any suggestions on how to resolve this would be great.
Replies
1
Boosts
0
Views
1.9k
Activity
Dec ’21
Beach ball and Memory Leak after completing code for Garden App Start when clicking "+" Button in Toolbar.
Hi, has anyone experienced the same problem? Also, while the New Plant menu command seems to work at least one time, the WaterPlants Menu command creates the same crash... Debug instrumentation shows a race condition in memory consumption, and the app needs to be force quit or the debug process terminated in Xcode. XCode Version is: 13.2 (13C90) MacOS Version is: 12.1 MacOS Build Target is: 12.0 Machine is: MacBook Air M1 2020, 8GB RAM, 256 GB SSD.
Replies
0
Boosts
0
Views
587
Activity
Dec ’21
Table() structure selection takes Object.ID but how to obtain reference to NSManagedObject from this ID
I am using the new SwiftUI Table() structure available for macOS. This structure takes a selection of multiple rows as a Set<Object.ID> and can be stored in a state property wrapper like so... @State private var selectedPeople = Set<Person.ID>() (per the example in the Apple documentation) Table() doesn't take any other type of identifier (that I am aware of), so I am constrained to use the ObjectIdentifier unique identifier. I am using Table() to present a list of NSManagedObjects. These NSManagedObjects are sourced from a Core Data Entity relationship property, in this case via a @FetchRequest. I want to be able to add new instances of the entity and also delete existing instances of the entity. I have worked out how to add a new instance - this is relatively easy. What I am struggling to work out is how to delete an NSManagedObject from the Table() by using the Object.ID that is used to track row selection. I have attempted this...     @State private var tableSelection = Set<Action.ID>() ... for actionID in tableSelection { let context = event.managedObjectContext let actionToDelete = context?.object(with: actionID) print(actionToDelete) context?.delete(actionToDelete) } but of course I have a type mismatch here and the compiler complains... Cannot convert value of type 'Action.ID' (aka 'ObjectIdentifier') to expected argument type 'NSManagedObjectID' So how I do grab a reference to the selected NSManagedObject from the set of Action.ID?
Replies
1
Boosts
0
Views
1.7k
Activity
Dec ’21