How can I auto add a new class instance to an array?
Example:
class Product {
var name: String?
}
var products = [Product]()
How can I add a new instance of a Product class to the products Array? How can I append to the array?
I tried some code but I don't know how to reference the class in own class.
I tried something like this:
class Product {
var name: String?
init() {
products.append(Produt)
}
var products = [Product]()
Thanks!
I dont't know why you need it, but you can use
If you want your newly created object stored in
products
array then you need to declare it asstatic
property so that it is shared by all instance otherwise it will just add first object for your every instance.Now use this
products
array usingProduct.products
.Have you tried
products.append(self)
?