chewy chewy - 1 year ago 194
Swift Question

Append to array in swift - has no member append?

What am I doing wrong in this code?

and why do I get the error ?

"Value of type 'NSMutableArray' has no member 'append'"

static func saveUserData()
let item:NSDictionary = ["TIME":"12:00","LOCATION":"here"]
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let filePath = documentsDirectory + "/datafile.dat"
var readArray:NSMutableArray? = NSMutableArray(contentsOfFile: filePath)

if var array = readArray {

print("loaded array - appending..." )

var damnArray:NSMutableArray = array
} else {

Answer Source

Because you are using NSMutableArray, you need to use addObject


append is used with swift array like this way

var arr = [Int]()

For more detail about swift array check this apple documentation or this tutorial.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download