içinde bir sözlük oluşturmak için azaltmak() kullanma işlevli bir program stili kullanarak sözlük oluşturmak istiyorum. Benim azaltmak() çalışmak için görünmüyor - Ben olsun: Sizin retVal
isteğe bağlıdırSwift
func loadMoveToCalendarsRules(calendarIndex: Int) -> [String]? {
// return something like ["phone call", "buzz", "ring"]
return NSUserDefaults.standardUserDefaults().objectForKey(generateMoveToCalendarsRules_NSUserDefaultsKey(calendarIndex)) as? [String]
}
// Add indeces to an array of any type
func addIndices<T>(toArray: [T]) -> [(index: Int, value: T)] {
return Array(zip(toArray.indices, toArray))
}
typealias CalendarRules = [EKCalendar : [String]]?
func buildCalendarRules(cals: [EKCalendar]) -> CalendarRules {
let sortedCals = cals.sort { $0.title.lowercaseString < $1.title.lowercaseString }
// build move to cal rules.
let indexedCalList = addIndices(sortedCals)
// go through the sorted calendars and build a dictionary that associates each calendar with a string array. (These are keywords that apply to the given calendar.)
let calendarRules = indexedCalList.reduce(nil as CalendarRules) {
accumulator, nextValue in
var retVal: [EKCalendar : [String]]? = accumulator
// if there are values found in NSUserDefaults for this calendar index then retrieve them.
if let rulesForCurrentCal = loadMoveToCalendarsRules(nextValue.index) {
retVal![nextValue.value] = rulesForCurrentCal // fatal error: unexpectedly found nil while unwrapping an Optional value
}
return retVal
}
print("------------ built calendar rules -------------")
print(Array(arrayLiteral: calendarRules?.keys))
print(Array(arrayLiteral: calendarRules?.values))
return calendarRules
}