Swift 4: 结构中的结构
Swift 4: struct in struct
我在创建结构时遇到问题。
我的结构:
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
希望有人能帮助我。
您忘记了调用 DeviceData.init(deviceID:type:attributes:)
的 deviceID:
命名参数,并且还忘记了 Meta.init(currentPage:nextPage:deviceID)
的 currentPage
和 nextPage
命名参数。
这是一个编译示例:
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"]
)
)
您省略了 DeviceData
和 Meta
初始值设定项的参数。在对您提出的另一个答案的评论中:
do I have to add them and set them to nil, even if they are optional? maybe that's my problem!
你可以这样做,例如类似于:
meta: Meta(currentPage: nil,
nextPage: nil,
deviceID: ["asfd-asdf-asdf-asdf-asdfcasdf"]
)
或者,您可以编写自己的初始化器而不是依赖默认的成员初始化器,并在那里提供默认值而不是在每次调用时提供默认值,例如类似于:
init(currentPage : Int? = nil, nextPage : Int? = nil, deviceID : [String]? = nil)
{
self.currentPage = currentPage
self.nextPage = nextPage
self.deviceID = deviceID
}
您原来的调用(省略了 currentPage
和 nextPage
)将有效并将这两个设置为 nil
。
HTH
我在创建结构时遇到问题。
我的结构:
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
希望有人能帮助我。
您忘记了调用 DeviceData.init(deviceID:type:attributes:)
的 deviceID:
命名参数,并且还忘记了 Meta.init(currentPage:nextPage:deviceID)
的 currentPage
和 nextPage
命名参数。
这是一个编译示例:
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"]
)
)
您省略了 DeviceData
和 Meta
初始值设定项的参数。在对您提出的另一个答案的评论中:
do I have to add them and set them to nil, even if they are optional? maybe that's my problem!
你可以这样做,例如类似于:
meta: Meta(currentPage: nil,
nextPage: nil,
deviceID: ["asfd-asdf-asdf-asdf-asdfcasdf"]
)
或者,您可以编写自己的初始化器而不是依赖默认的成员初始化器,并在那里提供默认值而不是在每次调用时提供默认值,例如类似于:
init(currentPage : Int? = nil, nextPage : Int? = nil, deviceID : [String]? = nil)
{
self.currentPage = currentPage
self.nextPage = nextPage
self.deviceID = deviceID
}
您原来的调用(省略了 currentPage
和 nextPage
)将有效并将这两个设置为 nil
。
HTH