无法将 ForEach 用于来自核心数据的不同记录
Unable to use ForEach for distinct records from coredata
我有一个 table,其中包含来自 5 位不同艺术家的 20 首歌曲,这个数字可以上下浮动,现在我想要一个视图,我可以在其中显示这些独特歌手的导航以及他们的图像也在 table.
所以我只需要基于艺术家姓名的不同记录,我查询 coredata 但是当我在 View 中使用它时,我得到 Generic struct 'ForEach' requires that 'NSFetchRequest<NSDictionary>' conform to 'RandomAccessCollection'
现在为了克服这个问题,我使用 id:.self,这也不起作用。
现在我阅读并了解到 ForEach 不喜欢任何未排序的 TYPE,但我无法找到解决方法,任何人都可以提出任何解决方案,谢谢。
这就是我从核心数据中获取唯一记录的方式
let fetchRequest: NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Artists")
init(songLVM: Binding<SongListVM>){
_songLVM = songLVM
fetchRequest.propertiesToFetch = ["artistname"]
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = .dictionaryResultType
}
下面是整个文件
import Foundation
import SwiftUI
import CoreData
struct ArtistList: View {
@Environment(\.managedObjectContext) var managedObjectContext
@Binding var songLVM: SongListVM
@State var namesFilter = [String]()
//-----
let fetchRequest: NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Artists")
init(songLVM: Binding<SongListVM>){
_songLVM = songLVM
fetchRequest.propertiesToFetch = ["artistname"]
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = .dictionaryResultType
}
//-----
var body: some View {
List {
ForEach(fetchRequest) { <---- Error here
idx in
NavigationLink(
destination: ArtistSongs(artistName: idx.artistname ?? "no name", songLVM: $songLVM)) {
ZStack(alignment: .bottomTrailing) {
Image(uiImage: UIImage(data: idx.artistImage ?? Data()) ?? UIImage())
.resizable()
.frame(width: 350, height: 350, alignment: .center)
Text(idx.artistname ?? "no name")
.font(Font.system(size: 50, design: .rounded))
.foregroundColor(.white)
}
}
}
}
}
}
一旦你弄清楚你的字典是什么,在你的 ForEach
循环中试试这个:
ForEach(dict.sorted(by: >), id: \.key) { key, value in
...
}
编辑 1:
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Artists.artistname, ascending: true)],
animation: .default)
private var artists: FetchedResults<Artists>
...
ForEach(artists, id: \.self) { artist in
...
}
我有一个 table,其中包含来自 5 位不同艺术家的 20 首歌曲,这个数字可以上下浮动,现在我想要一个视图,我可以在其中显示这些独特歌手的导航以及他们的图像也在 table.
所以我只需要基于艺术家姓名的不同记录,我查询 coredata 但是当我在 View 中使用它时,我得到 Generic struct 'ForEach' requires that 'NSFetchRequest<NSDictionary>' conform to 'RandomAccessCollection'
现在为了克服这个问题,我使用 id:.self,这也不起作用。
现在我阅读并了解到 ForEach 不喜欢任何未排序的 TYPE,但我无法找到解决方法,任何人都可以提出任何解决方案,谢谢。
这就是我从核心数据中获取唯一记录的方式
let fetchRequest: NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Artists")
init(songLVM: Binding<SongListVM>){
_songLVM = songLVM
fetchRequest.propertiesToFetch = ["artistname"]
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = .dictionaryResultType
}
下面是整个文件
import Foundation
import SwiftUI
import CoreData
struct ArtistList: View {
@Environment(\.managedObjectContext) var managedObjectContext
@Binding var songLVM: SongListVM
@State var namesFilter = [String]()
//-----
let fetchRequest: NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Artists")
init(songLVM: Binding<SongListVM>){
_songLVM = songLVM
fetchRequest.propertiesToFetch = ["artistname"]
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = .dictionaryResultType
}
//-----
var body: some View {
List {
ForEach(fetchRequest) { <---- Error here
idx in
NavigationLink(
destination: ArtistSongs(artistName: idx.artistname ?? "no name", songLVM: $songLVM)) {
ZStack(alignment: .bottomTrailing) {
Image(uiImage: UIImage(data: idx.artistImage ?? Data()) ?? UIImage())
.resizable()
.frame(width: 350, height: 350, alignment: .center)
Text(idx.artistname ?? "no name")
.font(Font.system(size: 50, design: .rounded))
.foregroundColor(.white)
}
}
}
}
}
}
一旦你弄清楚你的字典是什么,在你的 ForEach
循环中试试这个:
ForEach(dict.sorted(by: >), id: \.key) { key, value in
...
}
编辑 1:
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Artists.artistname, ascending: true)],
animation: .default)
private var artists: FetchedResults<Artists>
...
ForEach(artists, id: \.self) { artist in
...
}