swift

Classes and structures are the foundations of each program. Within classes and structures, the properties and methods are defined with the same syntax with which the variables and functions are defined.

Unlike other languages, where classes need to create a code for the interface and an implementation code, in Swift it is sufficient to define the body of the class.

The main difference between class and structure is that the first works for reference, the second for value. That is, when an instance of a class is created, a reference is made to the memory class occupied by the class, when an instance of a structure is created, the entire structure is copied to the new instance.

Defining a class or structure is syntactically the same. You need to pay attention to the conventions used in Swift. Classes and structures are types, so for the Swift language convention, the nomenclature uses the UpperCamelCase.

struct TomatoSauce {

    var tomato = 0

    var onion = 0

    var salt = 0

    var basil = 0

}

class PastaWithTomato {

    var sauce = TomatoSauce()

    var parmesan = false

    var pasta = 0

    var pastaType: String?

}

 

The above structure defines four stored properties initialized to 0. Due that we have initialized the four variables inside the declaration of the structure it is not necessary to specify the type as it is deduced from the compiler.

The PastaWithTomato class has four stored properties. The first, the sauce, is initialized with a new instance of the TomatoSauce structure, which also represents the type of the sauce variable. Parmesan is a false boolean variable. The amount of pasta is initialized to zero. The pastaType is not specified and is an optional variable (then ended with '?') of string type; when initializing, the compiler assigns the value nil.

At this time we have defined a structure and a class but no action has yet been done on them. To be able to act on classes and structures, it is necessary to instantiate and initialize them. Initialization is done by following the name of the class or structure with round parentheses..

var someSauce = TomatoSauce()

let somePasta = PastaWithTomato()

 

Some steps to access properties:

print(someSouce.basil) //It prints 0

print(somePasta.parmesan) //It prints false

print(somePasta.sauce.basil) //It prints 0

someSauce.basil = 4 //It assign 4 to the basil property

somePasta.sauce.basil = 4 //as above

 

The above code may seem trivial, but hides an insidious one. The first three lines print out properties that are present in the sauce struct and pasta class. At the fourth line, I modify the value of the basil property (assigning it to value 4) this is possible because someSouce has previously been defined as a variable; if I had defined it through the let keyword, that is, constant, this would give rise to an error.

On the contrary, the class somePasta, while being defined as constant, accepts the change of the value of the property. This is possible because with the instance of a class I refer to the class's address in memory, which has been defined constant, not the values contained in it. All this to highlight the difference between class and structure.

Two instances of the same structure are in fact two structures completely copied in the defined variables, the two variables are independent and unbound.

Two instances of the same class are two references that converge on the same data group (or memory location), consequently the changes I make to the properties of the first instance are seen on the second. To see if two variables refer to the same class instance, use the "identical" symbol consisting of the character '=' repeated three times (eg var1 === var2).

It is necessary to deepen the initialization theme because both the structure and the class have assigned default values. This method is valid and is also recommended for how it is handled by the compiler, but can only be used if you need to initialize the structure with the same values. In the case, whenever a class or structure is created, you want to attribute specific value properties it is necessary to use an initializer. See the example below.

enum Readers {

    case magnetic

    case rfid

    case combo

}

 

struct Terminal {

    let name: String

    var typeReader: Readers

    var display: Bool

    var keyboard: Bool

    

    init(name:String, typeReader: Readers, display: Bool, keyboard: Bool) {

        self.name = name

        self.typeReader = typeReader

        self.display = display

        self.keyboard = keyboard

    }

 

    init(name:String, display: Bool, keyboard: Bool) {

        self.name = name

        self.typeReader = Readers.combo

        self.display = display

        self.keyboard = keyboard

    }

}

 

var myTerm = Terminal(name: "TR1", typeReader: Readers.combo, display: true, keyboard: true)
var myTerm2 = Terminal(name: "TR2", display: true, keyboard: true)

 

In the above example there is another element to be underestimated: the ability to define multiple initializer with different parameters. Depending on the number or parameter label, the compiler will choose the appropriate initializer.