chewy chewy - 4 months ago 40
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..." )

// PROBLEM IS HERE
var damnArray:NSMutableArray = array
damnArray.append(item)
} else {
//
}
}

Answer

Because you are using NSMutableArray, you need to use addObject

damnArray.addObject(item)

append is used with swift array like this way

var arr = [Int]()
arr.append(5)

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

Comments