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 is swift stand for?

0 Answers  


What is memory leak in swift?

0 Answers  


What is swift module?

0 Answers  


How proficient are you in objective-c and swift? Can you briefly describe their differences?

0 Answers  


What is dynamic dispatch swift?

0 Answers  






What is the usage of switch statement in swift language?

0 Answers  


Do swift classes inherit from nsobject?

0 Answers  


What are the best ways of achieving concurrency in ios?

0 Answers  


How you define variables in swift?

0 Answers  


How does swift achieve multiple inheritance?

0 Answers  


What is encapsulation in swift?

0 Answers  


What is forced unwrapping?

0 Answers  


Categories