Search code examples
iosswiftxcodensuserdefaults

Unable to get NSMutableArray with multiple Object from UserDefaults in Swift


I am having custom class which stores the login information of the user. App can have multiple users logged in at a time.

This is how I am storing the logged in users data to UserDefaults.

let userDefaults = UserDefaults.standard
        let decoded = userDefaults.data(forKey: "CPContactInfo")

        if userDefaults.object(forKey: "CPContactInfo") != nil {
            let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! NSMutableArray
                arrayOfArray.add(qidInfo)
            }
        } else {
            print("userdefault not exist")
            arrayOfArray.add(qidInfo)
        }
        addQID(qid_info: arrayOfArray)

Where qidInfo is custom object of class QBM_CPContactInfo which I store it in array, at first I checked if UserDefault is present or not for given key at very first time, then added my qidInfo object into the array arrayOfArray and then that array is saved into UserDefault by calling function addQID(qid_info: arrayOfArray)

This is how my addQID function is,

func addQID(qid_info: NSMutableArray) {
        let userDefaults = UserDefaults()
        let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: qid_info)
        userDefaults.set(encodedData, forKey: "CPContactInfo")
        userDefaults.synchronize()
    }

After this when I add second logged in details it executes the

if userDefaults.object(forKey: "CPContactInfo") != nil

perfectly.

When I try to retrieve data from UserDefaults as below,

let userDefaults = UserDefaults.standard
        let decoded = userDefaults.data(forKey: "CPContactInfo")
        if userDefaults.object(forKey: "CPContactInfo") != nil {
            let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! NSMutableArray
            print(decodedTeams)
            qid_info = decodedTeams.object(at: 0) as! QBM_CPContactInfo
            print(qid_info?.qidEmail)
        }

it gives me only one object in return for decodedTeams as NSMutableArray, but actually I have added two items in NSMutableArray.

Can any one tell me whats I am doing wrong.

further I need to compare the email of user selected which is in let title = action.title along with qid_info?.qidEmail.

Any help will be appreciated. Thanks in advance.


Solution

  • You can have the following approach.

    let userDefaults = UserDefaults.standard let decoded = userDefaults.data(forKey: "CPContactInfo") if userDefaults.object(forKey: "CPContactInfo") != nil { let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! Array<QBM_CPContactInfo> arrayOfArray = decodedTeams arrayOfArray.append(qidInfo) } else { arrayOfArray.append(qidInfo) } addQID(qid_info: arrayOfArray)

    Here, first, get the saved Array from defaults in a temporary array and copy that temporary array in your existing array. Then append new elements into it and save the updated array for the same key.