gpt4 book ai didi

json - 在此上下文中,“D”对于类型查找不明确

转载 作者:行者123 更新时间:2023-11-30 11:03:03 25 4
gpt4 key购买 nike

我在使用 Xcode 10 的 Swift 4.2 中遇到以下问题

enter image description here

我正在创建所有模型类。 API 响应包含这种类型的 JSON 对象:

{  
"d":{
"results":[
{
"__metadata":{
"id":"urls"
}
}
]
}
}

如何创建具有相同名称的类而不产生歧义?

这是我的类实现:

import Foundation
import Alamofire

class SearchPINModel: Codable {
let d: D?

init(d: D?) {
self.d = d
}
}

class D: Codable {
let results: [Result]?

init(results: [Result]?) {
self.results = results
}
}

class Result: Codable {
let metadata: Metadata?
let sapID, deviceID, mobileNo, employeeName: String?
let pincode, sapUser, deviceCode: String?

enum CodingKeys: String, CodingKey {
case metadata = "__metadata"
case sapID = "SapId"
case deviceID = "DeviceId"
case mobileNo = "MobileNo"
case employeeName = "EmployeeName"
case pincode = "Pincode"
case sapUser = "SapUser"
case deviceCode = "DeviceCode"
}

init(metadata: Metadata?, sapID: String?, deviceID: String?, mobileNo: String?, employeeName: String?, pincode: String?, sapUser: String?, deviceCode: String?) {
self.metadata = metadata
self.sapID = sapID
self.deviceID = deviceID
self.mobileNo = mobileNo
self.employeeName = employeeName
self.pincode = pincode
self.sapUser = sapUser
self.deviceCode = deviceCode
}
}

class Metadata: Codable {
let id, uri: String?
let type: String?

init(id: String?, uri: String?, type: String?) {
self.id = id
self.uri = uri
self.type = type
}
}

最佳答案

你可以试试

struct Root: Codable {
let d: D
}

struct D: Codable {
let results: [Result]
}

struct Result: Codable {
let metadata: Metadata

enum CodingKeys: String, CodingKey {
case metadata = "__metadata"
}
}

struct Metadata: Codable {
let id: String
}

关于json - 在此上下文中,“D”对于类型查找不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53065941/

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