I'm using RealmSwift and I have a realm object called Book
Book
=====
title: String
pageCount: Int
when the app starts I get all the books using:
allBooks = realm.objects(Book)
a Book has a realm property called pageCount
I'm trying to understand how can I get the total pages count for all the books?
So if I have book A with 3 pages, book B with 11 pages and book C with 0 page the total count will be 3+11+0 = 14
I prefer doing it with a query and not iterate through all the books
You can get the total number of pages for all your book objects as follows:
let totalPages: Int = realm.objects(Book).sum("pageCount")
Swift 4+:
let totalPages: Int = realm.objects(Book.self).sum(ofProperty: "pageCount")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With