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

Mention some advantages of swift?

1 Answers  


How do I add a bridging header in swift?

1 Answers  


What is mvc architecture in swift?

1 Answers  


What is tuple in swift?

1 Answers  


Is swift open source?

1 Answers  


Explain the difference between let and var in swift programming?

1 Answers  


Which compiler is used in swift?

1 Answers  


What is static let in swift?

1 Answers  


How does swift achieve multiple inheritance?

1 Answers  


Explain some common features of protocols & superclasses.

1 Answers  


What is the latest version of swfit programming?

1 Answers  


What is static in swift?

1 Answers  


Categories