import Foundation class StringCalculator { func add(string:String) -> Int { let numbers = string.componentsSeparatedByString(",") var total = 0 for number in numbers { total += (number as NSString).integerValue } return total } } var x = StringCalculator() assert(x.add("1,2,3") == 6, "it handles lists of numbers.") assert(x.add("1") == 1, "it handles single numbers.") assert(x.add("") == 0, "it handles empty strings.") assert(x.add("1,,2") == 3, "it handles empty strings between numbers.") assert(x.add(",1,2,") == 3, "it handles empty strings on the ends.") assert(x.add(" ") == 0, "it handles whitespace") assert(x.add(" 1 ") == 1, "it handles whitespace around numbers") assert(x.add(" 1, 2 ,3 ") == 6, "it handles whitespace around commas")