[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

12 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
759
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
585
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
Xcode Code Along issue
Hi everyone! So i have recently decided to start watching apple code along tutorials. The one i've chosen is "SwfiftUI on the mac" and when i build the project, it fails and says: Signing for "Garden App" requires a development team. Select a development team in the Signing & Capabilities editor. I have specified the team (my account) and Signing Certificate, which is set to "Sign to run locally". I tried to set Xcode settings to default, but it didn't help.
2
0
836
Nov ’21
Cannot get [genreNames] into SwiftUI Table
This is a followup to SwiftUI Table does not work when trying to use Int values After getting a result from looking up an album or song in AppleMusic API, I parse the returned json using SwiftyJSON as shown here:  URLSession.shared.dataTask(with: musicRequest) { (data, response, error) in             guard error == nil else { return }             if let json = try? JSON(data: data!) {                 let result = (json["data"]).array!                 for songDetail in result {                     let attributes = songDetail["attributes"]                     let song = SongDetails(albumName: attributes["albumName"].string!,                                artistName: attributes["artistName"].string!,                                artworkURL: attributes["artwork"]["url"].string!,                                composerName: attributes["composerName"].string ?? "-",                                discNumber: attributes["discNumber"].int!,                                durationInMillis: attributes["durationInMillis"].int!,                                genreNames: attributes["genreNames"].arrayValue,                                isrc: attributes["isrc"].string!,                                name: attributes["name"].string!,                                id: attributes["playParams"]["id"].string!,                                releaseDate: attributes["releaseDate"].string!,                                trackNumber: attributes["trackNumber"].int!)                     songDetails.append(song)                     print(songDetails)                 } Notice how [genreNames] needs ".arrayValue" instead of ".string!". I Think the reason is because "genreNames" are listed in an Array of Strings. Here is the data model I use for the parsed results: struct SongDetails {     var albumName: String     var artistName: String     var artworkURL: String     var composerName: String     var discNumber: Int     var durationInMillis: Int     var genreNames: Array<Any>     var isrc: String     var name: String     var id: String     var releaseDate: String     var trackNumber: Int     init(albumName: String, artistName: String, artworkURL: String, composerName: String, discNumber: Int, durationInMillis: Int, genreNames: Array<Any>, isrc: String, name: String, id: String, releaseDate: String, trackNumber: Int) {         self.albumName = albumName         self.artistName = artistName         self.artworkURL = artworkURL         self.composerName = composerName         self.discNumber = discNumber         self.durationInMillis = durationInMillis         self.genreNames = genreNames         self.isrc = isrc         self.name = name         self.id = id         self.releaseDate = releaseDate         self.trackNumber = trackNumber      } } Here I have genreNames as "Array". This set up works and I can call and print jus the genreNames, but everything I have read online says that "[String]" should be used instead of "Array", but if I do that, I get an error in the previous code when using [genreNames].string!. The way I currently have it set up, genreNames show up in the console as genreNames: [Pop, Music] , so I think it is correct. The issue arises when I try to use genreNames in a SwiftUI view. In my SwiftUI view I have a Table where these results go.             HStack {                 WebImage(url: URL(string: songDetail.artworkURL.replacingOccurrences(of: "{w}", with: "5000").replacingOccurrences(of: "{h}", with: "5000")))                     .resizable()                     .indicator(.activity)                     .frame(width: 400, height: 400)                     .cornerRadius(5)                     .shadow(radius: 2)                                  VStack (alignment: .leading) {                     Text("song id: \(songDetail.id)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("title: \(songDetail.name)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("artist: \(songDetail.artistName)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("composer: \(songDetail.composerName)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("disc number \(songDetail.discNumberString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("track number: \(songDetail.trackNumberString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("duration in ms: \(songDetail.durationInMillisString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("release date: \(songDetail.releaseDate)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("isrc: \(songDetail.isrc)")                         .foregroundColor(.secondary)                         .padding(2)                 }                 .font(.system(size: 14))                 }             }         }.padding(.leading, 20.0)         .frame(minWidth:800, idealWidth: 1000, maxWidth: .infinity, minHeight:600, idealHeight: 800, maxHeight: .infinity)         .onAppear {             SKCloudServiceController.requestAuthorization { (status) in                 if status == .authorized {                     self.searchResults = AppleMusicAPI().getSongDetails(self.songId)                 }             }         }     } } extension SongDetails {     var discNumberString: String {         String(discNumber)     }     var trackNumberString: String {         String(trackNumber)     }     var durationInMillisString: String {         String(durationInMillis)     } } The extension at the bottom is how the issue of the original post was solved. It seems that the new SwiftUI Table can only accept String values. To get the genreNames into this table I tried making a similar extension where I used join() to try and make a string of the genreResults, but that did not work either. I can't remember the exact code, but it could have been wrong. So how would this be accomplished? Anyone know? This is the last thing that I need to get into my Table. Thanks! I appreciate the help!
1
0
1.1k
Nov ’21
Using Core Data in a new to macOS 12 (beta) `Table`
My project is a universal app with iOS 15 and macOS 12 targets, to attempt to take advantage of latest technologies, including SwiftUI and Table. I let Xcode take care of the CodeGen by selecting type "Class Definition" and then write Extensions for custom properties. I have an entity named Event with a class name PTG_Event. Each Event can have a number of actions and so each Event also has a one-to-many relationship with Action (class PTG_Action). I am attempting to use Table to display the actions for each event... but this doesn't work... struct ActionTable: View {     @ObservedObject var event: PTG_Event     @FetchRequest(entity: PTG_Action.entity(),                   sortDescriptors: []     ) var eventActions: FetchedResults<PTG_Action>     @State var sortOrder: [SortDescriptor<PTG_Action>] = [         .init(\.dateCommences, order: SortOrder.forward)     ]     var body: some View {         let events = eventActions.filter { $0.event == event }         Text("Action Table")         Table(events, sortOrder: $sortOrder) {             TableColumn("Ref", value: \.reference)             { action in                 Text(action.reference ?? "NO REF")             }             TableColumn("Date", value: \.dateCommences)             { action in                 Text(action.dateCommences?.formatted(date: .abbreviated, time: .omitted) ?? "NO DATE")             }         }     } } I have attempted many variations on this. Any suggestions?
2
0
1.7k
Oct ’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
759
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
585
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
Xcode Code Along issue
Hi everyone! So i have recently decided to start watching apple code along tutorials. The one i've chosen is "SwfiftUI on the mac" and when i build the project, it fails and says: Signing for "Garden App" requires a development team. Select a development team in the Signing & Capabilities editor. I have specified the team (my account) and Signing Certificate, which is set to "Sign to run locally". I tried to set Xcode settings to default, but it didn't help.
Replies
2
Boosts
0
Views
836
Activity
Nov ’21
Cannot get [genreNames] into SwiftUI Table
This is a followup to SwiftUI Table does not work when trying to use Int values After getting a result from looking up an album or song in AppleMusic API, I parse the returned json using SwiftyJSON as shown here:  URLSession.shared.dataTask(with: musicRequest) { (data, response, error) in             guard error == nil else { return }             if let json = try? JSON(data: data!) {                 let result = (json["data"]).array!                 for songDetail in result {                     let attributes = songDetail["attributes"]                     let song = SongDetails(albumName: attributes["albumName"].string!,                                artistName: attributes["artistName"].string!,                                artworkURL: attributes["artwork"]["url"].string!,                                composerName: attributes["composerName"].string ?? "-",                                discNumber: attributes["discNumber"].int!,                                durationInMillis: attributes["durationInMillis"].int!,                                genreNames: attributes["genreNames"].arrayValue,                                isrc: attributes["isrc"].string!,                                name: attributes["name"].string!,                                id: attributes["playParams"]["id"].string!,                                releaseDate: attributes["releaseDate"].string!,                                trackNumber: attributes["trackNumber"].int!)                     songDetails.append(song)                     print(songDetails)                 } Notice how [genreNames] needs ".arrayValue" instead of ".string!". I Think the reason is because "genreNames" are listed in an Array of Strings. Here is the data model I use for the parsed results: struct SongDetails {     var albumName: String     var artistName: String     var artworkURL: String     var composerName: String     var discNumber: Int     var durationInMillis: Int     var genreNames: Array<Any>     var isrc: String     var name: String     var id: String     var releaseDate: String     var trackNumber: Int     init(albumName: String, artistName: String, artworkURL: String, composerName: String, discNumber: Int, durationInMillis: Int, genreNames: Array<Any>, isrc: String, name: String, id: String, releaseDate: String, trackNumber: Int) {         self.albumName = albumName         self.artistName = artistName         self.artworkURL = artworkURL         self.composerName = composerName         self.discNumber = discNumber         self.durationInMillis = durationInMillis         self.genreNames = genreNames         self.isrc = isrc         self.name = name         self.id = id         self.releaseDate = releaseDate         self.trackNumber = trackNumber      } } Here I have genreNames as "Array". This set up works and I can call and print jus the genreNames, but everything I have read online says that "[String]" should be used instead of "Array", but if I do that, I get an error in the previous code when using [genreNames].string!. The way I currently have it set up, genreNames show up in the console as genreNames: [Pop, Music] , so I think it is correct. The issue arises when I try to use genreNames in a SwiftUI view. In my SwiftUI view I have a Table where these results go.             HStack {                 WebImage(url: URL(string: songDetail.artworkURL.replacingOccurrences(of: "{w}", with: "5000").replacingOccurrences(of: "{h}", with: "5000")))                     .resizable()                     .indicator(.activity)                     .frame(width: 400, height: 400)                     .cornerRadius(5)                     .shadow(radius: 2)                                  VStack (alignment: .leading) {                     Text("song id: \(songDetail.id)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("title: \(songDetail.name)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("artist: \(songDetail.artistName)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("composer: \(songDetail.composerName)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("disc number \(songDetail.discNumberString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("track number: \(songDetail.trackNumberString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("duration in ms: \(songDetail.durationInMillisString)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("release date: \(songDetail.releaseDate)")                         .foregroundColor(.secondary)                         .padding(2)                     Text("isrc: \(songDetail.isrc)")                         .foregroundColor(.secondary)                         .padding(2)                 }                 .font(.system(size: 14))                 }             }         }.padding(.leading, 20.0)         .frame(minWidth:800, idealWidth: 1000, maxWidth: .infinity, minHeight:600, idealHeight: 800, maxHeight: .infinity)         .onAppear {             SKCloudServiceController.requestAuthorization { (status) in                 if status == .authorized {                     self.searchResults = AppleMusicAPI().getSongDetails(self.songId)                 }             }         }     } } extension SongDetails {     var discNumberString: String {         String(discNumber)     }     var trackNumberString: String {         String(trackNumber)     }     var durationInMillisString: String {         String(durationInMillis)     } } The extension at the bottom is how the issue of the original post was solved. It seems that the new SwiftUI Table can only accept String values. To get the genreNames into this table I tried making a similar extension where I used join() to try and make a string of the genreResults, but that did not work either. I can't remember the exact code, but it could have been wrong. So how would this be accomplished? Anyone know? This is the last thing that I need to get into my Table. Thanks! I appreciate the help!
Replies
1
Boosts
0
Views
1.1k
Activity
Nov ’21
Using Core Data in a new to macOS 12 (beta) `Table`
My project is a universal app with iOS 15 and macOS 12 targets, to attempt to take advantage of latest technologies, including SwiftUI and Table. I let Xcode take care of the CodeGen by selecting type "Class Definition" and then write Extensions for custom properties. I have an entity named Event with a class name PTG_Event. Each Event can have a number of actions and so each Event also has a one-to-many relationship with Action (class PTG_Action). I am attempting to use Table to display the actions for each event... but this doesn't work... struct ActionTable: View {     @ObservedObject var event: PTG_Event     @FetchRequest(entity: PTG_Action.entity(),                   sortDescriptors: []     ) var eventActions: FetchedResults<PTG_Action>     @State var sortOrder: [SortDescriptor<PTG_Action>] = [         .init(\.dateCommences, order: SortOrder.forward)     ]     var body: some View {         let events = eventActions.filter { $0.event == event }         Text("Action Table")         Table(events, sortOrder: $sortOrder) {             TableColumn("Ref", value: \.reference)             { action in                 Text(action.reference ?? "NO REF")             }             TableColumn("Date", value: \.dateCommences)             { action in                 Text(action.dateCommences?.formatted(date: .abbreviated, time: .omitted) ?? "NO DATE")             }         }     } } I have attempted many variations on this. Any suggestions?
Replies
2
Boosts
0
Views
1.7k
Activity
Oct ’21