-2

I am trying input an array with all the months and display them one by one on my scrollable calendar. I am trying to return each individual "month" for each header of the calendar.

I am using the following string array, yet i am unsure how to return a type of "String" for what i desire. Any ideas on what i am doing wrong? Thanks

let months: [String] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

func monthCounter(inputArray:Array<String>) -> String{
    for name in inputArray{
        return name

    }


} 

I am wanting to call this function to display each "month" string individually.

func calendar(_ calendar: JTAppleCalendarView, willDisplaySectionHeader header: JTAppleHeaderView, range: (start: Date, end: Date), identifier: String) {


        let headerCell = (header as? MonthsHeader)
        headerCell?.monthsHeader.text = monthCounter(inputArray: months)
    }
}
Travis Whitten
  • 44
  • 2
  • 11

2 Answers2

-2
headerCell?.monthsHeader.text = monthCounter[index of section]
Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18
-3

There are serveral problems with your code. First of all, Array<String> should be [String] and returning from inside an iteration will terminate the iteration. You can call a function there, or you can print("\(name)") for a start to see if your code is working.

As I wrote in my comments below, I think your approach is not correct. Your delegate function does not work that way. Even if a single monthCounter call was able to return multiple times, you would overwrite the same text attribute over and over again. Look for the right delegate function to use that passes in the month for the header that will display. See my comments for a possible hint.

Lupinity Labs
  • 2,099
  • 1
  • 15
  • 23
  • 1
    It is ok to have `Array`. We have `public struct Array : RandomAccessCollection, MutableCollection` – Rajan Maheshwari Nov 03 '16 at 02:57
  • You're absolutely right. [String] is just a shorthand version of Array. I thought it was strange, however, using [String] as a type for months and then Array in the function declaration. – Lupinity Labs Nov 03 '16 at 03:05
  • Gotcha, but what should i return after the for loop bracket? Thanks – Travis Whitten Nov 03 '16 at 03:41
  • I'm not sure if you are using the right delegate function for what you want to achieve. You need to know which month to return the header text for. You might want to check out `func calendar(_ calendar: JTAppleCalendarView, sectionHeaderIdentifierFor range: (start: Date, end: Date), belongingTo month: Int) -> String` where you get a month parameter passed in what you can use directly to determine which month name you need (e.g. just do a `return months[month]`) – Lupinity Labs Nov 03 '16 at 03:49