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.
// 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. If you want an Optional Int, then declare it Int?
.
That said, I would be very thoughtful about creating so many Optionals, and particularly optional arrays and strings. Is there a difference in your system between "no inventory" ( nil
) and "an empty list of inventory" ( []
)? Or "no name" ( nil
) and "empty name" ( ""
)? 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:
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:
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
. There are some exceptions, such as Identifiable structs, which should make their id
be let
because their id
has some special meaning. But as a rule, making structs excessively let
just makes them hard to use without gaining any benefits. (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.
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. 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 this
struct Main: Codable {
var meta: Meta?
var objects: [Object]?
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.