1

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 {
           //
         }
    }
1
  • append it is only available to Swift native array type.
    – Leo Dabus
    Jul 28, 2016 at 16:25

3 Answers 3

12

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.

1
  • Can you please tell me the difference between declaring an array like var arr : [[String : Any]]? and var arr = [[String : Any]](). I am so much confused in these. Please help me with it.
    – iPeter
    Mar 29, 2018 at 14:53
1

Your had declared dammArray as NSMutableArray. So there is no method append is available. Append is available in swift array object.

 var damnArray:NSMutableArray
 damnArray.addObject(item)

or

 var damnArray = [NSDictionary]()
 damnArray.append(item) 
0

As per your answer and syntax this worked for me thanks, in my code which is:

@IBAction func btnSend(_ sender: UIButton)

{

    //data.append(tfData.text!)

    data.add(tfData.text!)
    tblViewList.reloadData()

    tfData.resignFirstResponder()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.