|
| 1 | +// |
| 2 | +// MACAddress.swift |
| 3 | +// DataDecoder |
| 4 | +// |
| 5 | +// Created by Kevin Hoogheem on 4/1/17. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +import Foundation |
| 26 | + |
| 27 | + |
| 28 | +public struct MACAddress { |
| 29 | + |
| 30 | + fileprivate(set) public var stringValue: String |
| 31 | + |
| 32 | + public var dataValue: Data { |
| 33 | + let substring = stringValue.replacingOccurrences(of: ":", with: "") |
| 34 | + //let da = Data(bytes: [Character](substring.characters).byteArray) |
| 35 | + |
| 36 | + return substring.data(using: .utf8)! |
| 37 | + } |
| 38 | + |
| 39 | + public init(string: String) { |
| 40 | + self.stringValue = string.uppercased() |
| 41 | + } |
| 42 | + |
| 43 | + public init(data: Data) { |
| 44 | + |
| 45 | + self.stringValue = "00:00:00:00:00:00" |
| 46 | + |
| 47 | + if data.count == 6 { |
| 48 | + self.stringValue = String(format: "%02X:%02X:%02X:%02X:%02X:%02X", |
| 49 | + data[0], |
| 50 | + data[1], |
| 51 | + data[2], |
| 52 | + data[3], |
| 53 | + data[4], |
| 54 | + data[5]).uppercased() |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +extension MACAddress: Equatable { |
| 60 | + |
| 61 | + public static func ==(lhs: MACAddress, rhs: MACAddress) -> Bool { |
| 62 | + return (lhs.stringValue == rhs.stringValue) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension MACAddress: Hashable { |
| 67 | + |
| 68 | + public var hashValue: Int { |
| 69 | + return stringValue.hashValue |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
0 commit comments