How to initialize an empty struct? - arrays

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 vars, 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]?
}

Related

How to modify the value of key in nested array of struct in swift5?

How to modify the value of key from struct that is under nested of array of struct. I found one of possible solution from stackoverflow but it is only for one level. I am wondering if there is any better solution? and what if the key is optional?
struct Article {
var id: Int
var book: [Book]
}
struct Book {
var page: Int
var author: [Author]
}
struct Author {
var visited: Bool
}
// update value of visited key in nested array of struct
var a = Article(id: 1, book: [Book(page: 11, author: [Author(visited: true)])])
print(a)
a.book.modifyElement(atIndex: 0) {$0.author.modifyElement( atIndex: 0) {$0.visited = false}}
print(a)
Changing The value of struct in an array
"How to modify the value of property from struct that is under nested of array of struct".
Try this approach, a better more compact solution than what you have:
var a = Article(id: 1, book: [Book(page: 11, author: [Author(visited: true)])])
print("\n---> before a: \(a)")
a.book[0].author[0].visited = false // <-- here
print("\n---> after a: \(a)")

how to access a member of struct that is a member of array in swift

I have an Array called items1 its member's a struct Team1 with 2 members id: UUID() and round: Int I want to make a function that counts the sum of the round member of the array. can anyone help me understand what I'm doing wrong in my code below:
import Foundation
struct vrb {
static var items1 = [
Team1(id: UUID(), round: 14),
Team1(id: UUID(), round: 20),
Team1(id: UUID(), round: 24),
]
static var total1: Int = 0
}
func Total() -> Int {
var sum = vrb.items1[round.reduce(0, +)]
}
struct Team1: Identifiable {
var id: UUID
var round: Int
}
You're somewhat close. What you're looking for is this:
func total() -> Int {
vrb.items1.map(\.round).reduce(0, +)
}
Map ("transform each element of") items1 to its .round values, and then sum those.
(Note that functions, methods, and variables should have a leading lowercase letter, and types like structs should have a leading uppercase letter. This makes it easier for other Swift developers to understand your code.)

Unable to append string to mutable array in Swift

Context - I'm currently learning Swift Struct. So I decided to create my own Phone structure in Playground. (see below)
Problem -
The downloadApp method on the phone struct is throwing the following error.. Cannot use mutating member on immutable value: 'self' is immutable.
Expect outcome - To peacefully append new strings to my apps property, which is an array of strings.
Swift code
struct Phone {
let capacity : Int
let Brand : Sting
var name: String
let model: String
var apps: [String]
var contacts: [String]
var formatCapacity: String {
return "\(capacity)GB"
}
func downloadApp(name: String){
apps.append(name) // ERROR
}
}
You simply need to mark downloadApp as mutating. The issue is caused by the fact that downloadApp is declared in a struct type, which is a value type and hence if you mutate any properties (namely, the apps array here) of the struct, you actually mutate the struct itself. By marking the function as mutating, the compiler allows such modification of the struct through a member function of the type.
mutating func downloadApp(name: String){
apps.append(name)
}

Append to generic array in Swift

I have a protocol named Foo and a struct named Bar. Bar conforms to Foo.
protocol Foo {}
struct Bar: Foo {}
Appending a Bar instance to an array of Bar works as expected.
var array = [Bar]()
array.append(Bar())
Now, I have a generic struct called Baz that's initialized with a type that conforms to Foo (e.g. Bar).
struct Baz<T: Foo> {
private(set) var array = [T]()
init() {
if T.self is Bar.Type {
// Error: Cannot invoke 'append' with an argument list of type (Bar)
array.append(Bar())
}
}
}
Attempting to append to the array results in the following error:
Cannot invoke 'append' with an argument list of type (Bar)
Why doesn't this work as expected?
As an aside, the use case is something like this:
let bazBarStack = Baz<Bar>().array
let bazQuxStack = Baz<Qux>().array
You need to store objects in array, that are of type T. Therefore you should cast using as! T:
struct Baz<T: Foo> {
private(set) var array = [T]()
init() {
if T.self is Bar.Type {
array.append(Bar() as! T)
}
}
}
Because array holds elements of a single type T, which must conform to Foo but is not necessarily compatible with Bar. It looks like you wish to have an array that can hold anything conforming to Foo; in this case, get rid of the generics altogether and simply write
private(set) var array = [Foo]()
and then your init will be
init() {
array.append(Bar())
}

SWIFT how to store a CLASS containing a STRUCT with an ARRAY of STRUCT inside USERDEFAULTS

the problem may sound weird but is exactly my situation.
I have a class containing a struct with only an array inside: an array of another struct. I would like to store an object of the class into user default. Let see some code
struct Inside {
var something: String
var somethingElse: Int
}
struct Outside {
var array = [Inside]()
}
class TheClass: {
var array = [Outside]()
}
var a = TheClass()
//now I would like to put 'a' into UserDefaults
I tried to use NSCoding, like you can see in this code (the code compiles)
class TheClass: NSObject, NSCoding {
var array: NSMutableArray = NSMutableArray()
override init(){
self.elementi = []
}
required init(coder aDecoder: NSCoder) {
elementi = aDecoder.decodeObjectForKey("elementi") as! NSMutableArray
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(elementi, forKey: "elementi")
}
}
but when I try to add an object of struct Outside into the NSMutableArray, I get an error. I found out that one solution is to convert into an NSValue. Here is the code in objective C, I am not able to translate it in swift
//here 'a' denotes an object of Outside struct that we want to insert into the NSMutableArray stored inside an object of TheClass (we cannot add it directly to the NSMutableArray)
NSValue* value = [NSValue value:&a withObjCType:#encode(Outside)];
//To extract it later:
[value getValue:&value];
I don't know how to solve this problem. Maybe I can change the struct to classes, in this way NSObject is no more need and I can use NSCoding directly (can be used with class object but not with struct object). But if I do this way I will get exactly the error that this guy have: SWIFT: Copying objects from arrays then changing properties and adding them back to original array
What do you suggest?

Resources