UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Need help with a couple of computed properties

Forums > SwiftUI

Hi, I'm working on a SwiftUI project where I have a core data Order item set up to keep track of some invoices. I have a total (Decimal) attribute for each order to display in a list with a FetchRequest and some NSPredicates set up for filtering in a variety of ways. My Order item also has attributes for invoiceNumber (Interger 64) and received (Boolean).

I figured out that I could use a computed property to get the grand total from the displayed orders by using:

var grandTotal: Decimal {
     orders.filteredList.reduce(Decimal(0.0)) {
          if let amount = $1.total as Decimal? {
               return $0 + amount
          }
          return $0
     }
}

But I need a couple of other things and I'm not sure how to get them. One would be to return a receivedTotal: Decimal that would only sum up the orders displayed where the received is true. The other thing I need is a way to generate a nextInvoiceNumber: Int for the next higest sequential invoice number so I need a way to figure out from all the records what the highest number currently is so I can add 1 to it. Are computed properties the best way to go about this (or is there some better solution for getting this information from Core Data?)

Anyone have any suggestions or can you point to any tutorials that might offer some clues that I could go check out?

1      

I might have figured out the nextInvoiceNumber but not sure if this is the best solution. Still no closer on trying to figure out the receivedTotal.

var nextInvoiceNumber: Int64 {
     let invoiceNumberSet = Set(ordersList.compactMap{$0.invoiceNumber} as [Int64])
     return (invoiceNumberSet.max() ?? 0) + 1
}

I know the Set may seem unnecessary, but I might have several invoices where no number was provided at the time that might all be 0 so figured having a Set will narrow it down to just a single 0 and might be more efficient as it gets the max value?

1      

You could consider this for some pointers.

struct ContentView: View {
    let numberList = [7, 4, -8, 21, 16, 25, 12, -33, 31, 49]

    var body : some View {    
        let totalSum = numberList.filter{ ($0 > 20) }.reduce(0, +)
        Text("Total sum \(totalSum)")
    }
}

2      

Thanks @Greenamberred. Still not sure I'm entirely getting it right, but this is what I came up with for receivedTotal (it does work):

var receivedTotal: Decimal {
     orders.filteredList.filter{($0.cleared)}.reduce(Decimal(0.0)) {
          if let amount = $1.total as Decimal? {
               return $0 + amount
          }
          return $0
     }
}

I had forgotten about .filter

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.