Alejandro
Alejandro

Reputation: 220

Append use in swift

Why the code bellow does not change the string?

class Person {

var name = "Chris"
var age = 12
var male = true
var details, = [true, 100 , "good"]

}
family().details.append("friendly")

family().details //prints [1, 100, "good"]

Upvotes: 0

Views: 57

Answers (1)

Diego Freniche
Diego Freniche

Reputation: 5414

Because you're accessing two different objects:

family().me.append("friendly")

This line creates an object, appending "friendly" to your [Any]

family().me //prints [1, 100, "good"]

Here you're printing the contents of a brand new object: me contains only 3 elements

If you want to see the change use the same object like so:

let f = family()

f.me.append("friendly")
f.me

Upvotes: 5

Related Questions