puuurm / swift-weatherforecast

iOS app
1 stars 0 forks source link

Apply map, flatMap #25

Closed puuurm closed 6 years ago

puuurm commented 6 years ago

map, flatMap을 사용하여 기존 코드 수를 줄인다. 예) 변경 전:

func temperatures(at index: Int) -> [Float] {
    var temps = [Float]()
    forecastStores[index].weekly?.forecasts.forEach { temps.append($0.mainWeather.temperature.rounded()) }
    return temps
}

변경 후:

func temperatures(at index: Int) -> [Float] {
    return forecastStores[index].weekly?.forecasts.map { $0.mainWeather.temperature.rounded() } ?? []
}