Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.46 KB | None | 0 0
  1. func stringToInt(_ string: String) -> Int {
  2.     let digitVal: [Character : Int] = ["0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9]
  3.    
  4.     var negative = false
  5.     var result = 0
  6.    
  7.     for char in string {
  8.         if let digit = digitVal[char] {
  9.             result = result * 10 + digit
  10.         }
  11.         else if char == "-" {
  12.             negative = true
  13.         }
  14.     }
  15.    
  16.     return negative ? -result : result
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement