Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func stringToInt(_ string: String) -> Int {
- let digitVal: [Character : Int] = ["0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9]
- var negative = false
- var result = 0
- for char in string {
- if let digit = digitVal[char] {
- result = result * 10 + digit
- }
- else if char == "-" {
- negative = true
- }
- }
- return negative ? -result : result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement