Posts

Post not yet marked as solved
0 Replies
363 Views
It just a simple contact book app, and there are some model stored through SwiftData import Foundation import SwiftData protocol aData: Equatable, Identifiable { var label: String { get set } var value: String { get set } init(label: String, value: String) } @Model class aNumber: aData, Identifiable { var id = UUID() var label: String var value: String required init(label: String, value: String) { self.label = label self.value = value } } @Model class aEmail: aData, Identifiable {... // here are sample with aNumber} @Model class aURL: aData, Identifiable {...} @Model class anAddress: aData, Identifiable {...} @Model class Contact: Identifiable { var id = UUID() var _firstName: String = "" var _lastName: String = "" var firstName: String { get { return _firstName } set { _firstName = newValue updateNameForSort() } } var lastName: String { get { return _lastName } set { _lastName = newValue updateNameForSort() } } var fullName: String { return _lastName + _firstName } var pinyinName: String { return (...//some rules)} var nameForSort: String var company: String var numbers: [aNumber] var emails: [aEmail] var URls: [aURL] var dates: [Date] var remarks: String var tags: [String] init(firstName: String, lastName: String, company: String, numbers: [aNumber], emails: [aEmail], URls: [aURL], dates: [Date], remarks: String, tags: [String]) { ... } convenience init() { self.init(firstName: "", lastName: "", company: "", numbers: [], emails: [], URls: [], dates: [], remarks: "", tags: []) } func getFullName() -> String { return self._lastName+self._firstName } func updateNameForSort() { nameForSort = ...//some rules } } And the ListView import Foundation import SwiftUI import SwiftData struct BookView: View { @Environment(\.modelContext) private var context @Query(sort: \Contact.nameForSort) private var Contacts: [Contact] @State var isEditing = false var body: some View { let contactsDctionary = Dictionary(grouping: Contacts, by: {$0.nameForSort.prefix(1).uppercased()}) List { ForEach(contactsDctionary.sorted(by: { $0.key < $1.key }), id: \.key) { key, contacts in // group by initials Section(header: Text(key)) { ForEach(contacts, id: \.self) { Contact in { ...//some view NavigationLink(destination: ContactView(contact: Contact)) {} .opacity(0) } } } } } .listStyle(InsetListStyle()) .navigationBarTitle("All", displayMode: .large) .toolbar { Button(action: { isEditing.toggle() }) { Image(systemName: "plus") } } .sheet(isPresented: $isEditing) { NavigationView { ContactEditor(originalData: nil, isEditing: $isEditing) } } } } And the contactView from navigationLink: import Foundation import SwiftUI struct ContactView: View { var contact: Contact @State var isEditing = false var body: some View { List { Section { ForEach(contact.numbers) { aNumber in row(aData: aNumber) } } Section { ForEach(contact.emails) { aEmail in row(aData: aEmail) } } } .navigationTitle(contact.fullName) .toolbar { Button(action: { isEditing.toggle() }) { Text("编辑") } } .sheet(isPresented: $isEditing) { NavigationStack { ContactEditor(originalData: contact, isEditing: $isEditing) } } } } Now I had some contacts had stored, they store some [aData] like the numbers: [aNumber], when I view them in contactView, their elements, such a list of the "aNumber" are arranged in some order(this is random), then I completely close the app and open it next time, the order of the array elements displayed just now has been completely disrupted. So how to solve this problem? I just want to keep their order the same as when I append them in array. In addition, English is not my mother ******. I hope you can understand my expression. I would be very grateful if anyone can solve this problem!
Posted
by lykyk595.
Last updated
.