简体   繁体   English

如何初始化一个空结构?

[英]How to initialize an empty struct?

I'm lost at how to initialize the struct below?我不知道如何初始化下面的结构? Basically I want to create an empty variable that represents the struct below, but the values should be optional/nil, but i have no idea how to go about it.基本上我想创建一个代表下面结构的空变量,但值应该是可选的/nil,但我不知道如何 go 关于它。

// MARK: - Main
struct Main: Codable {
    let meta: Meta
    let objects: [Object]
}

// MARK: - Meta
struct Meta: Codable {
    let status: String
    let count, offset, totalcount: Int
}

// MARK: - Object
struct Object: Codable {
    let id:Int
    let url: String
    let displayname: String
    let inventory: [Inventory]
    let media: [Media]
}


// MARK: - Inventory
struct Inventory: Codable {
   let id, warehouseid, instock, threshold: Int
    let reserved: Int
    let coordinates, note: String
    let prodno: Int
    let lastinventory: String
    let soldout, onpurchaseorders, generation: Int
    let created, changed: String
}

// MARK: - Media
struct Media: Codable {
    let id, prodno: Int
    let webpath: String
    let slot: Int
    let type, extdata: String
    let ctime, xsize, ysize, mediasetid: Int
    let alt, title: String
    let generation: Int
    let created, changed: String
}

With the below code I get the error message "no exact matches in call to initializer, and a bunch of other errors if i change the code.使用下面的代码,我收到错误消息“调用初始化程序时没有完全匹配,如果我更改代码,还会出现一堆其他错误。

var sökresultat3 = Main3()

If you want the properties to be Optionals, then you have to declare them that way.如果您希望属性是可选的,那么您必须以这种方式声明它们。 Int means you want an Int. Int表示你想要一个 Int。 If you want an Optional Int, then declare it Int?如果你想要一个可选的 Int,那么将它声明为Int? . .

That said, I would be very thoughtful about creating so many Optionals, and particularly optional arrays and strings.也就是说,我会非常考虑创建这么多的 Optional,尤其是可选的 arrays 和字符串。 Is there a difference in your system between "no inventory" ( nil ) and "an empty list of inventory" ( [] )? “无库存”( nil )和“库存的空列表”( [] )在您的系统中是否存在差异? Or "no name" ( nil ) and "empty name" ( "" )?还是“无名”( nil )和“空名”( "" )? If these are the same, then you often will prefer to just have default values, not optional values.如果这些相同,那么您通常会更喜欢只使用默认值,而不是可选值。 Optionals add a lot of complexity that you may not need.选项增加了很多您可能不需要的复杂性。

As an example of default values, using var will make this much simpler and give you the initializers you're hoping for:作为默认值的示例,使用var将使这更简单,并为您提供您希望的初始化程序:

struct Main: Codable {
    var meta = Meta()
    var objects: [Object] = []
}

// MARK: - Meta
struct Meta: Codable {
    var status = ""
    var count = 0
    var offset = 0
    var totalcount = 0
}

If you do make these optional, then var again will create the initializers you're looking for:如果您确实将这些设为可选,那么var将再次创建您正在寻找的初始化程序:

struct Main: Codable {
    var meta: Meta?
    var objects: [Object]?
}

As a rule, simple data structs that have no logic in them should declare their properties var .通常,没有逻辑的简单数据结构应该声明它们的属性var There are some exceptions, such as Identifiable structs, which should make their id be let because their id has some special meaning.有一些例外,例如 Identifiable structs,应该让他们的idlet ,因为他们的id有一些特殊的含义。 But as a rule, making structs excessively let just makes them hard to use without gaining any benefits.但通常,使结构过度let只会使它们难以使用而不会获得任何好处。 (Values types are never shared, so mutation is not a problem.) (值类型从不共享,因此变异不是问题。)

If you still want to use let , you'll need to define your own init by hand.如果您仍想使用let ,则需要手动定义自己的init

If all your struct's properties are optionals, and you make them all var s, Swift will create an intializer that defaults all your properties to nil.如果您的结构的所有属性都是可选的,并且您将它们全部设为var ,Swift 将创建一个初始化器,将您的所有属性默认为 nil。 It is as if you defined your struct as follows:就好像您将结构定义如下:

struct AStruct {
    var a: Int?
    var string: String?
    
    //You don't have to define this initializer if all your properties are var Optionals
    init(a: Int? = nil, string: String? = nil) {
        self.a = a
        self.string = string
    }
}

You can invoke that initializer like this:您可以像这样调用该初始化程序:

var aStruct = AStruct()

As Alexander pointed out, just because you can do this does not mean that you should do it.正如亚历山大所指出的,仅仅因为你做到这一点并不意味着你就应该这样做。 It does not seem like good practice.这似乎不是一个好的做法。

Change main to thismain更改为此

struct Main: Codable {
var meta: Meta?
var objects: [Object]?
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM