1

如何为笔记建模,以便我可以在之后按日期搜索它们?就像图片上的一样。

首先,我想到了带有 type 键的字典Date;但这对我不起作用。所以我尝试了以下String格式:

“04-28-2020” - 美国日历

"28-04-2020" - 其他日历

问题之一 - 如果电话更改日期样式(取决于地区),键仍保留在字典中,但不再被访问......

我的直觉应该是字典,但是制作这样一个模型的可持续方法是什么?它可以是一个[Note]没有键的音符数组吗?

在此处输入图像描述

struct Note: Codable, Identifiable {
    let id = UUID()
    var date: Date
    var title: String
    var description: String
    
    var dateString: String {
        let df = DateFormatter()
        df.dateStyle = .short
        df.timeStyle = .medium
        return df.string(from: date)
    }
    
    // method returns "key" to be used in dictionary
    static func dateKey(for date: Date) -> String {
        let df = DateFormatter()
        df.dateStyle = .short
        return df.string(from: date)
    }
}

class NotesOrganizer: ObservableObject {
    
    // @Published var notes = [Note]() - tried initially
    @Published var notesDictionary = [String : [Note]]()
}

struct ContentView: View {
    @State private var title = ""
    @State private var description = ""
    @ObservedObject var notesOrganizer = NotesOrganizer()
    
    var body: some View {
        NavigationView {
            VStack {
                Button("Save note") {
                   
                        let key = Note.dateKey(for: Date())
                        notesOrganizer.notesDictionary[key]?.append(Note(date: Date(), title: title, description: description)) 
?? (notesOrganizer.notesDictionary[key] = [Note(date: Date(), title: title, description: description)])
                        
                        print("Date Key: \(Note.dateKey(for: Date()))")    
                }
                
                
                NavigationLink(destination: DetailView(notesOrganizer: notesOrganizer)) 
                {
                    
                    Text("Log history ->")
                        .foregroundColor(.purple)
                        .underline()
                }
            }
            .navigationBarHidden(true)

        }
        
    }
    
    init() {
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        UINavigationBar.appearance().shadowImage = UIImage()
    }

}

详细视图()

extension Date {
        var tomorrow: Date? {
            return Calendar.current.date(byAdding: .day, value: 1, to: self)
        }
        
        var yesterday: Date? {
            return Calendar.current.date(byAdding: .day, value: -1, to: self)
        }
}

struct DetailView: View {
    @ObservedObject var notesOrganizer: NotesOrganizer
    @State private var dayLabel: String = Note.dateKey(for: Date())
    @State private var chosenDate = Date()
    

    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    chosenDate = chosenDate.yesterday!
                    dayLabel = Note.dateKey(for: chosenDate)
                }, label: {
                    Image(systemName: "chevron.left")
                })
                
                Text(dayLabel)
                
                Spacer()
                
                Button(action: {
                    chosenDate = chosenDate.tomorrow!
                    dayLabel = Note.dateKey(for: chosenDate)
                }, label: {
                    Image(systemName: "chevron.right")
                })
                
            }
            .padding([.horizontal,.bottom])
            
            List{
                ForEach(notesOrganizer.notesDictionary[day] ?? []) { note in
                    HStack {
                        VStack(alignment:.leading) {
                            Text(note.title)
                            Text(note.description)
                        }.multilineTextAlignment(.leading)
                        Spacer()
                        Text(note.dateString)
                    }
                }.onDelete(perform: removeItems)
            }
            .navigationBarTitle("", displayMode: .inline)
                
            
            Spacer()
        }
4

0 回答 0