SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top