Reputation: 358
Following How to limit the number of results in a FetchRequest in SwiftUI I created a FetchRequest to get the latest billing number.
My code looks like this
var billNo: FetchRequest<Payments>
...
init() {
let request: NSFetchRequest<Payments> = Payments.fetchRequest()
request.fetchLimit = 1
request.sortDescriptors = [NSSortDescriptor(key: "rechnungsNummer", ascending: false)]
billNo = FetchRequest<Payments>(fetchRequest: request)
}
But how on earth do I get access to the values of billNo.rechnungsNummer
? billNo.wrappedValue.first?.rechnungsNummer
ist always nil
.
Or has it all changed the last two years?
btw: it's all inside a view. ;)
Upvotes: 1
Views: 218
Reputation: 358
Thanks to vadian for the missing hint.
Here's my working code:
@FetchRequest var billNo: FetchedResults<Payments>
...
init() {
let request: NSFetchRequest<Payments> = Payments.fetchRequest()
request.fetchLimit = 1
request.sortDescriptors = [NSSortDescriptor(key: "rechnungsNummer", ascending: false)]
_billNo = FetchRequest<Payments>(fetchRequest: request)
}
And in body, I can access this easily with
billNo.first?.rechnungsNummer
I hope it might help other beginners like me. :-)
Upvotes: 1