Search code examples
swiftuiios14xcode12

Trouble Reading and Formatting Date


I'm attempting to read some data from CoreData into a standard list and have trouble reading and formatting the date. I appear to be way off in the weeds! I have the error "Value of type 'Date?' has no member 'getFormattedDate'" where I'm trying to display the date and the error "Cannot convert value of type 'listView' to expected argument type 'Date'" at the last line of the getFormattedDate function.

struct listView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    let categories = ["AAA", "BBB","CCC","DDD"]
    
    @FetchRequest(
        entity: CurrTrans.entity(),
        sortDescriptors: []
    ) var currTrans: FetchedResults<CurrTrans>
    
    var body: some View {
        
        List {
            ForEach(currTrans, id: \.self) { item in
                VStack {
                    let date = item.entryDT
                    Text(date.getFormattedDate())
                      
                    // display category
                    if item.entryCat != nil {
                        Text("Category: \(categories[Int(item.entryCat)])")
                    } else {
                        Text("")
                    
                    // display description
                    if item.entryDsc != nil {
                        Text("\(item.entryDsc!)")
                    } else {
                        Text("")
                    }
                }
            }
        }
    
    
        func getFormattedDate() -> String {
            let dateformat = DateFormatter()
            dateformat.dateFormat = "d MMM HH:mm"
            return dateformat.string(from: self)
        }
    }
}

Solution

  • @objc(Transaction)
    public class Transaction: NSManagedObject {
        static let categoryStringsByCategoryID = ["AAA", "BBB","CCC","DDD"]
        func categoryString() -> String{
             return Transaction.categoryStringsByCategoryID[self.categoryID]
        }
    }
    
    struct ListView: View {
        
        @Environment(\.managedObjectContext) private var viewContext
        
        @FetchRequest(sortDescriptors: []) var transactions: FetchedResults<Transaction>
        
        var body: some View {
            
            List {
                ForEach(transactions) { transaction in
                    VStack {
                        Text("\(transaction.timestamp!, formatter: transactionFormatter)")
                        // display category
                        
                        Text("Category: \(transaction.categoryString)")
                        
                        // display description
                        Text("\(transaction.entryDescription ?? "")")
                    }
                }
            }
        }
    }
    
    private let transactionFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .medium
        return formatter
    }()