Using objects as properties of another class Swift 3

2

I am new to Swift, I have a class that has properties that belong to other classes.

class Pizza {
    var tamaño=Tamaño.self
    var masa=Masa.self
    var queso=Queso.self
    var ingredientes = [Ingrediente]()
    var aa:String = ""
}

The problem is that I can not assign them values, I get the following error "Can not assign value of type 'Size' to type 'Size.Type'"

var pi:Pizza=Pizza()
var t:Tamaño=Tamaño(nombre: "min", precio: 25)
pi.tamaño=t

When it is another type of data such as Int, String or a Struct it leaves me but when they are objects of a class that I have created, it does not leave me. Does Swift allow it? What is my fault? Can I only do it with Struct? Greetings.

    
asked by Víctor 20.08.2017 в 01:01
source

1 answer

3

With the aa property it works correctly because you have determined that it is of the String class and you have given it a String value, but in the other properties you have given it a value instead of an object, so after that it gives an error to the try to assign an object as a value. With: you determine the class and with = you assign the value

class Pizza {
    var tamano : Tamano?
    var masa : Masa?
    var queso : Queso?
    var ingredientes : Ingrediente?
    var aa : String = ""
}
    
answered by 20.08.2017 в 16:33