0001 // Int.swift 0002 // 0003 // The MIT License (MIT) 0004 // 0005 // Copyright (c) 2015 Zewo 0006 // 0007 // Permission is hereby granted, free of charge, to any person obtaining a copy 0008 // of this software and associated documentation files (the "Software"), to deal 0009 // in the Software without restriction, including without limitation the rights 0010 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0011 // copies of the Software, and to permit persons to whom the Software is 0012 // furnished to do so, subject to the following conditions: 0013 // 0014 // The above copyright notice and this permission notice shall be included in all 0015 // copies or substantial portions of the Software. 0016 // 0017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0022 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0023 // SOFTWARE. 0024 0025 extension Int { 0026 public init?(hexString: String) { 0027 struct Error: ErrorType {} 0028 0029 let map = [ 0030 "0": 0, "1": 1, "2": 2, "3": 3, 0031 "4": 4, "5": 5, "6": 6, "7": 7, 0032 "8": 8, "9": 9, "A": 10, "B": 11, 0033 "C": 12, "D": 13, "E": 14, "F": 15 0034 ] 0035 0036 let number = try? hexString.uppercaseString.unicodeScalars.reduce(0) { 0037 let character = String($1) 0038 if character == " " { return $0 } 0039 guard let digit = map[character] else { throw Error() } 0040 return $0 * 16 + digit 0041 } 0042 0043 if let number = number { 0044 self.init(number) 0045 } else { 0046 return nil 0047 } 0048 } 0049 }
String.swift:45 guard let character = Int(hexString: hexString) else {