Convert Degrees North to Cardinal Directions January 25, 2016

In my Weather App I pull weather data from an API that returns wind direction in Degrees from North, and I needed the corresponding cardinal directions to display "10mph West", etc.

Referencing a C# solution to this problem, I wrote a Swift function to convert degrees North to cardinal directions.

            func convertDegreesNorthToCardinalDirection(degrees: Int) -> String {
    
        let cardinals: [String] = [ "North",
                                    "Northeast",
                                    "East",
                                    "Southeast",
                                    "South",
                                    "Southwest",
                                    "West",
                                    "Northwest",
                                    "North" ]
    
        let index = Int(round(Double(degrees).truncatingRemainder(dividingBy: 360) / 45))

        return cardinals[index]
        
      }
          

That's it! Have fun and #CompileSwift.