gpt4 book ai didi

swift - 在 Swift 中设置枚举案例的 rawValue?

转载 作者:行者123 更新时间:2023-11-30 10:56:42 29 4
gpt4 key购买 nike

我正在尝试在下面的结构中设置 var VendorIDDeviceID 的 rawValue。

struct vendor: Codable {
var VendorIDDeviceID: String

enum CodingKeys: String, CodingKey {
case VendorIDDeviceID = "Vendor\(vendorID)Device\(deviceID)"
}
}

但是我需要能够在编码过程中填写vendorID和deviceID的变量。我已经在互联网的深处进行了搜索,但找不到任何与执行此类操作相关的内容。我发现的最接近的是:

    let vendorID = "1002"
let deviceID = "67df"
let enumCase = vendor.VendorIDDeviceID(rawValue: "Vendor\(vendorID)Device\(deviceID)")

但不知道如何在编码过程中实现这一点。任何帮助将不胜感激。

最佳答案

以下是我尝试解析 https://pastebin.com/raw/LALX2PvV 中的 JSON 的方法由于 MACPro 对象中的键事先未知,因此它使用动态键并将解析结果存储在字典中。

struct BundleData: Codable {
let cfBundleName, cfBundleIdentifier,
cfBundleInfoDictionaryVersion, osBundleRequired: String
let cfBundleVersion, cfBundleExecutable,
cfBundleGetInfoString, cfBundleSignature: String
let cfBundlePackageType: String
let ioKitPersonalities: IOKitPersonalities
let buildMachineOSBuild, lsMinimumSystemVersion,
cfBundleDevelopmentRegion, nsHumanReadableCopyright: String
let cfBundleShortVersionString: String

enum CodingKeys: String, CodingKey {
case cfBundleName = "CFBundleName"
case cfBundleIdentifier = "CFBundleIdentifier"
case cfBundleInfoDictionaryVersion = "CFBundleInfoDictionaryVersion"
case osBundleRequired = "OSBundleRequired"
case cfBundleVersion = "CFBundleVersion"
case cfBundleExecutable = "CFBundleExecutable"
case cfBundleGetInfoString = "CFBundleGetInfoString"
case cfBundleSignature = "CFBundleSignature"
case cfBundlePackageType = "CFBundlePackageType"
case ioKitPersonalities = "IOKitPersonalities"
case buildMachineOSBuild = "BuildMachineOSBuild"
case lsMinimumSystemVersion = "LSMinimumSystemVersion"
case cfBundleDevelopmentRegion = "CFBundleDevelopmentRegion"
case nsHumanReadableCopyright = "NSHumanReadableCopyright"
case cfBundleShortVersionString = "CFBundleShortVersionString"
}
}

struct IOKitPersonalities: Codable {
let agpm: Agpm

enum CodingKeys: String, CodingKey {
case agpm = "AGPM"
}
}

struct Agpm: Codable {
let cfBundleIdentifier, ioProviderClass, ioClass: String
let machines: Machines
let ioNameMatch: String

enum CodingKeys: String, CodingKey {
case cfBundleIdentifier = "CFBundleIdentifier"
case ioProviderClass = "IOProviderClass"
case ioClass = "IOClass"
case machines = "Machines"
case ioNameMatch = "IONameMatch"
}
}

struct Machines: Codable {
let macPro41, macPro51: MACPro

enum CodingKeys: String, CodingKey {
case macPro41 = "MacPro4,1"
case macPro51 = "MacPro5,1"
}
}

struct MACPro: Codable {
var defaultControlID: Int
let devices: [String: DeviceInfo]

enum CodingKeys: String, CodingKey {
case defaultControlID = "default-control-id"
}

struct DeviceKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) {
self.stringValue = "\(intValue)";
self.intValue = intValue
}
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DeviceKey.self)

self.defaultControlID = 0
var devices = [String: DeviceInfo]()
for key in container.allKeys {
if let device = try? container.decode(DeviceInfo.self, forKey: key) {
devices[key.stringValue] = device
} else if let controlId = try? container.decode(Int.self, forKey: key) {
self.defaultControlID = controlId
}
}

self.devices = devices
}
}

struct DeviceInfo: Codable {
let heuristic: Heuristic
let logControl, controlID: Int

enum CodingKeys: String, CodingKey {
case heuristic = "Heuristic"
case logControl = "LogControl"
case controlID = "control-id"
}
}

struct Heuristic: Codable {
let thresholdLow: [Int]
let idleInterval, sensorSampleRate, targetCount, sensorOption: Int
let thresholdHigh: [Int]
let id: Int

enum CodingKeys: String, CodingKey {
case thresholdLow = "Threshold_Low"
case idleInterval = "IdleInterval"
case sensorSampleRate = "SensorSampleRate"
case targetCount = "TargetCount"
case sensorOption = "SensorOption"
case thresholdHigh = "Threshold_High"
case id = "ID"
}
}

let data = json.data(using: .utf8)!
do {
let bundleData = try JSONDecoder().decode(BundleData.self, from: data)
print(bundleData)
}
catch {
print(error)
}

关于swift - 在 Swift 中设置枚举案例的 rawValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53891438/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com