1.

Explain Why A Compile Time Error Occurs. How Can You Fix It?

Answer»

STRUCTURES are value types. By DEFAULT, the properties of a value type cannot be modified from WITHIN its instance methods.

However, you can optionally allow such modification to OCCUR by declaring the instance methods as ‘mutating’;

EX:

struct IntStack {

var items = [Int]()

mutating func add(x: Int) {

items.append(x) // All good!

}

}

Structures are value types. By default, the properties of a value type cannot be modified from within its instance methods.

However, you can optionally allow such modification to occur by declaring the instance methods as ‘mutating’;

EX:

struct IntStack {

var items = [Int]()

mutating func add(x: Int) {

items.append(x) // All good!

}

}



Discussion

No Comment Found