1

I have the following code for PreviewProvider for TransactionsListView.swift:

struct TransactionsListView_Previews: PreviewProvider {

    static var previews: some View {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let transaction = NPTransaction(context: context)
        transaction.date = Date()
        transaction.income = true
        transaction.type = "morning"
        transaction.value = 1200
        transaction.notes = "notes"
        return TransactionsListView(filter: true, startDate: Date()).environment(\.managedObjectContext, context)
    }

}

Preview is not showing up. There is a message that Build Succeeded and there is no error message, but Canvas is empty. What am I doing wrong?

P.s. I am using CoreData and have 1 entity called NPTransaction which has 5 attributes: date, income, notes, type, value.

mallow
  • 2,368
  • 2
  • 22
  • 63

1 Answers1

0

As Xcode didn't give me any errors, it was not easy to guess. I needed to add addingTimeInterval to Date() in Previews. Working code:

struct TransactionsListView_Previews: PreviewProvider {

static var previews: some View {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let transaction = NPTransaction(context: context)
    transaction.date = Date()
    transaction.income = true
    transaction.type = "morning"
    transaction.value = 1200
    transaction.notes = "notes"
    return TransactionsListView(filter: true, startDate: Date().addingTimeInterval(-86400 * 30)).environment(\.managedObjectContext, context)
}

}

Kudos to @CranialDev who helped me here: https://stackoverflow.com/a/59970329/12315994

mallow
  • 2,368
  • 2
  • 22
  • 63