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]?
}
Assume I have an array of structs like so:
struct Record {
let name: String
}
let array = [Record(name: "John"), Record(name: "Jim"), Record(name: "Bob")]
I would like to get the index of each element using UILocalizedIndexedCollation.section(for: collationStringSelector:). The problem is, when I pass:
#selector(getter: record.name)
the following error is returned:
Argument of '#selector' refers to var 'name' that is not exposed to
Objective-C
Is there any way of exposing an instance value in a struct to a #selector? (NB: the struct I am passing is used extensively throughout my app and I don't really want to change it to a class)
Converting the struct variable to an NSString and using one of NSString's methods / variables is a work around that fixed the issue:
let index = UILocalizedIndexedCollation.current().section(for: (record.name as NSString), collationStringSelector: #selector(getter: NSString.uppercased))
What I'm looking to try to do is have another var attached to the array to store values in without having to make a 2D array.
var list: NSMutableArray! = NSMutableArray()
list.add("Apple")
list[0].color = red // Something like this
Any help in the right direction will be appreciated
For that make array of custom class or struct.
struct Fruit {
let name: String
let color: String
}
Now make array of this struct and add object of this struct in it.
var fruits = [Fruit]()
fruits.append(Fruit(name: "Apple", color: "red"))
Now you can access object in a way you describe in your question.
print(fruits[0].name) //Apple
print(fruits[0].color) //red
Note: In Swift use swift's type Array and Dictionary instead of NSArray and NSDictionary.
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())
}
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?