The following code snippet results in a compile time error:

struct IntStack {

var items = [Int]()

func add(x: Int) {

items.append(x) // Compile time error here.

}

}

Explain why a compile time error occurs. How can you fix it?



The following code snippet results in a compile time error: struct IntStack { var items = [I..

Answer / iosraj

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’; e.g.:

struct IntStack {

var items = [Int]()

mutating func add(x: Int) {

items.append(x) // All good!

}

}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Apple iOS Swift Interview Questions

What are lazy stored properties, and how are they useful?

0 Answers  


What is final class in swift?

0 Answers  


How is swift different from objective-c?

0 Answers  


How to convert nsarray to nsmutablearray in swift?

0 Answers  


What is lazy in swift?

0 Answers  






What is swift module?

0 Answers  


What is mutating keyword in swift?

0 Answers  


What is a function in swift?

0 Answers  


What is a static variable swift?

0 Answers  


Is swift a functional language?

0 Answers  


Why is swift important?

0 Answers  


What is core data swift?

0 Answers  


Categories