swift - Trouble using width for String in for-in cycle -


i've write simple code:

extension string {     func trailingspaces (width: int) -> string {         var s = "\(self)"         in count(s)..<width {             s = s + " "         }         return s     }      func leadingspaces (width: int) -> string {         var s = "\(self)"         in count(s)..<width {             s = " " + s         }         return s     } }  class viewcontroller: uiviewcontroller {      var users = ["marco", "gianni", "antonio", "giulio", "franco"]     var ages = [29, 45, 17, 33, 37]      override func viewdidload() {         super.viewdidload()          var merged = [string: int] ()         var totalage = 0.0          var = 0; < ages.count; i++ {             merged[users[i]] = ages[i]         }          user in sorted(merged.keys) {             let age = merged[user]             totalage += double(age!)             let paddeduser = user.trailingspaces(10)             let paddedage = "\(age)".leadingspaces(3)             println("\(paddeduser) \(age!)")         }          println("\n\(merged.count) users")         println("average age: \(totalage / double(merged.count))")     } } 

but can't make work leadingspaces function , can't understand reason, it's quite identical other extension func works.

it give error

fatal error: can't form range end < start

on runtime

in case run kind of problem, println() of variable using

println("\(age)") right before let paddedage = "\(age!)".leadingspaces(3) reveals problem

age optional, meaning trying padding on string has value "optional(17)"

thus, count(s) higher 3, , have invalid range


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -