0

I've implemented an NSFetchedResultsController correctly so that it splits up my fetched data by sections. When I try print out its property sectionIndexTitles I just get "0", but if I iterate over its sections and print out the section names it prints out each section's name:

dump(fetchedResultsController.sectionIndexTitles)
    // prints out "0"
var names = [String]()
for val in (fetchedResultsController.sections)! {
    names.append(val.name)
}
dump(names)
    // prints out:  - "03/31/2017"
                    - "04/01/2017"
                    - "04/02/2017"

I thought the sectionIndexTitles returns an array of all the sections' names, is that incorrect?

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • The `sectionIndexTitles` relate to the index that can be used in table views - see the Contacts app for example - showing (normally) the initial letters of the names of each section. Because your section names all begin with "0", that is the only index title that is required. – pbasdf Apr 18 '17 at 10:50
  • Ok, So I guess to make an array of the actual section names I need to loop over my sections like I'm doing in my code above. – MarksCode Apr 18 '17 at 18:52
  • Do you need a separate array? Why not just use the frc.sections array and access the name property when necessary? – pbasdf Apr 18 '17 at 20:00
  • I do need an array, I'm using the iOS charts library that needs an array for a certain function. – MarksCode Apr 18 '17 at 20:03
  • Ah. You could use .map to avoid looping: `let sectionNames = fetchedResultsController.sections!.map { $0.name }`. – pbasdf Apr 18 '17 at 20:08
  • I'm pretty sure `.map` loops anyways, how else would it iterate over every value? – MarksCode Apr 18 '17 at 20:12
  • Yes, you're right. Just slightly neater to code/read? Up to you. – pbasdf Apr 18 '17 at 20:14

0 Answers0