Travis Whitten Travis Whitten - 1 month ago 15
Swift Question

Swift String Array Iterating Function

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)
}
}

Answer

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.