Last active
August 29, 2015 14:16
-
-
Save jeden/e51ca85320638927cd87 to your computer and use it in GitHub Desktop.
Convert an array of Int to an array of their respective string representations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static let digitNames = | |
[0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", | |
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"] | |
extension Array { | |
func translateToDigitalNames()-> [String]? { | |
let numbers: [Int] = self.filter { $0 is Int }.map { $0 as Int } | |
if numbers.count != self.count { | |
return nil | |
} | |
return Array.translateToDigitalNames(numbers) | |
} | |
private static func translateToDigitalNames(numbers: [Int])-> [String] { | |
return numbers.map { self.translateToDigitalName($0) } | |
} | |
private static func translateToDigitalName(var number: Int) -> String { | |
var output = "" | |
while number > 0 { | |
output = digitNames[number % 10]! + output | |
number /= 10 | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment