You are on page 1of 1

struct Device {

private static let devices: [String: String] = {


let json = Bundle.current.url(forResource: "Devices", withExtension:
"plist")!
let data = try! Data(contentsOf: json)
let decoder = PropertyListDecoder()
return try! decoder.decode([String: String].self, from: data)
}()

private static func deviceName(for device: String) -> String {


return devices[device] ?? device
}

static var deviceName: String {


#if canImport(Darwin)
#if os(macOS)
let hwmodel = "hw.model"
#else
let hwmodel = "hw.machine"
#endif
var size: Int = 0
sysctlbyname(hwmodel, nil, &size, nil, 0)
var machine = [UInt8].init(repeating: 0, count: size)
sysctlbyname(hwmodel, &machine, &size, nil, 0)
return deviceName(for: String(cString: machine))
#else
#error ("Not supported")
#endif
}
}

class Decoy: NSObject { }

extension Bundle {
static var current: Bundle = Bundle(for: Decoy.self)
}

You might also like