gpt4 book ai didi

swift 4 : struct in struct

转载 作者:搜寻专家 更新时间:2023-11-01 06:13:05 28 4
gpt4 key购买 nike

我在创建结构时遇到问题。

我的结构:

public struct Device: Codable {
let data: DeviceData
let meta: Meta?
}

public struct DeviceData: Codable {
let deviceID: String?
let type: String?
let attributes: Attributes?

private enum CodingKeys: String, CodingKey {
case deviceID = "id"
case type
case attributes
}
}

public struct Attributes: Codable {
let name: String?
let asdf: String?
let payload: Payload?
}

public struct Payload: Codable {
let example: String?
}

public struct Meta: Codable {
let currentPage: Int?
let nextPage: Int?
let deviceID: [String]?
}

当我现在想创建这个结构的一个元素时:

var exampleData = Device(
data: DeviceData(
type: "messages",
attributes: Attributes(
name: "Hello World",
asdf: "This is my message",
payload: Payload(
example: "World"
)
)
),
meta: Meta(
deviceID: ["asfd-asdf-asdf-asdf-asdfcasdf"]
)
)

我会得到一个错误。无法详细说明此错误,因为当我删除“meta”元素时,因为它是可选的,所以会发生另一个错误...此特定代码的错误消息是:

Extra argument 'meta' in call

我希望有人能帮助我。

最佳答案

您忘记了 deviceID: 调用 DeviceData.init(deviceID:type:attributes:) 的命名参数,并且您也忘记了 currentPagenextPage 将参数命名为 Meta.init(currentPage:nextPage:deviceID)

这是一个编译示例:

var exampleData = Device(
data: DeviceData(
deviceID: "someID",
type: "messages",
attributes: Attributes(
name: "Hello World",
asdf: "This is my message",
payload: Payload(
example: "World"
)
)
),
meta: Meta(
currentPage: 123,
nextPage: 456,
deviceID: ["asfd-asdf-asdf-asdf-asdfcasdf"]
)
)

关于 swift 4 : struct in struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51026854/

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