大家好,新年快樂
我在這個站點上學會了創建字典來根據物件的屬性準備串列部分,例如:
struct Lieu: Identifiable, Hashable {
var id = UUID().uuidString
var nom: String
var dateAjout = Date()
var img: String
var tag: String
var tag2: String
var tag3: String
}
準備以“標簽”為關鍵字的字典:
private var lieuxParTag: [String: [Lieu]] {
Dictionary(grouping: listeDeLieux) { lieu in
lieu.tag
}
}
private var lieuxTags: [String] {
lieuxParTag.keys.sorted(by: <)
}
然后在視圖中,它很簡單:
ForEach (lieuxTags, id: \ .self) {tag in
Section (header: Text (tag)) {
ScrollView (.horizontal, showsIndicators: true) {
LazyHStack {
ForEach (lieuxParTag [tag]!) {Place in
Text (place.name)
}
}
}
}
但是如果“Lieu”包含這樣的標簽屬性,如何制作部分:
var tags: [String]
在這張表中,我整合了“Lieu”的所有標簽,例如:
Lieu(nom: Paris, tags: ["tour eiffel", "bouchons"])
謝謝你的幫助。
uj5u.com熱心網友回復:
標準庫中沒有現成的解決方案,但您可以輕松地自己實作。您遍歷物件,并為每個物件遍歷其標簽并將當前物件添加到當前標簽的字典中:
var dictionary: [String: [Place]] = [:]
for place in places {
for tag in place.tags {
dictionary[tag, default: []].append(place)
}
}
uj5u.com熱心網友回復:
這是一個帶注釋的作業示例。我還稍微收緊了您的代碼。
struct SectionsFromArrayView: View {
@State private var listeDeLieux: [Lieu] = Lieu.previewData
private var lieuxParTag: [String: [Lieu]] {
// This first loop extracts the different tags from the array into a set
// to unique them so we only iterate each tag once in the next step.
var tagSet: Set<String> = []
for lieu in listeDeLieux {
for tag in lieu.tags {
tagSet.insert(tag)
}
}
// This loop puts together the dictionary by filtering listeDeLieux by tag
var parTag: [String: [Lieu]] = [:]
for tag in Array(tagSet).sorted() {
let lieuArray = listeDeLieux.filter( { $0.tags.contains(tag) })
parTag[tag] = lieuArray
}
return parTag
}
var body: some View {
List {
// Instead of having a separate computed variable lieuxTags, you can sort the
// array of the lieuxParTag.keys
ForEach(lieuxParTag.keys.sorted(by: <), id: \ .self) {tag in
Section(header: Text (tag)) {
ScrollView(.horizontal, showsIndicators: true) {
LazyHStack {
ForEach(lieuxParTag[tag]!) { lieu in
Text (lieu.nom)
}
}
}
}
}
}
}
}
struct Lieu: Identifiable, Hashable {
var id = UUID().uuidString
var nom: String
var dateAjout = Date()
var img: String
// By making tags a set, they are uniqued
var tags: Set<String>
static var previewData: [Lieu] = [
Lieu(nom: "Alfred", img: "pencil", tags: ["pencil", "doc"]),
Lieu(nom: "Ben", img: "pencil", tags: ["pencil"]),
Lieu(nom: "Charles", img: "paperplane", tags: ["paperplane", "doc"]),
Lieu(nom: "Alfred", img: "paperplane", tags: ["paperplane"]),
Lieu(nom: "Alfred", img: "pencil", tags: ["pencil", "doc"])
]
}
如果您可以將標簽設為列舉,則可以進一步壓縮代碼,因為您已經擁有了有限的集合。這進一步縮小了lieuxParTag:
struct SectionsFromArrayView: View {
@State private var listeDeLieux: [Lieu] = Lieu.previewData
private var lieuxParTag: [Tag: [Lieu]] {
var parTag: [Tag: [Lieu]] = [:]
// In order to get an array, Tag.allCases, Tag must conform to CaseIterable
for tag in Tag.allCases {
let lieuArray = listeDeLieux.filter( { $0.tags.contains(tag) })
parTag[tag] = lieuArray
}
return parTag
}
var body: some View {
List {
// In order to sort the keys, Tag must conform to Comparable
ForEach (lieuxParTag.keys.sorted(), id: \ .self) {tag in
Section(header: Text(tag.rawValue)) {
ScrollView(.horizontal, showsIndicators: true) {
LazyHStack {
ForEach(lieuxParTag[tag]!) { lieu in
Text(lieu.nom) Text(Image(systemName: lieu.img))
}
}
}
}
}
}
}
}
struct Lieu: Identifiable, Hashable {
var id = UUID().uuidString
var nom: String
var dateAjout = Date()
var img: String
// By making tags a set, they are uniqued
var tags: Set<Tag>
static var previewData: [Lieu] = [
Lieu(nom: "Alfred", img: Tag.pencil.rawValue, tags: [.pencil, .doc]),
Lieu(nom: "Ben", img: Tag.pencil.rawValue, tags: [.pencil]),
Lieu(nom: "Charles", img: Tag.paperplane.rawValue, tags: [.paperplane, .doc]),
Lieu(nom: "Alfred", img: Tag.paperplane.rawValue, tags: [.paperplane]),
Lieu(nom: "Alfred", img: Tag.pencil.rawValue, tags: [.pencil, .doc])
]
}
enum Tag: String, CaseIterable, Comparable {
case doc, paperplane, pencil
static func < (lhs: Tag, rhs: Tag) -> Bool {
lhs.rawValue < rhs.rawValue
}
}
這是一個很好的第一個問題。我唯一推薦的是始終提供一個最小的、可重現的示例。你想讓別人幫忙盡可能容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400498.html
