Saving Array of String to User Defaults then Saving, Synchronising, Appending & Removing Strings from the same Array

Hi Folks,


I am building iOS app that stores data in Array of strings from other array of selected strings using append method.


But when I quit app all stored data in array get lost. Can somebody let me know how to store data in User Defaults using synchronisation. Then how to retrive, append, set & remove data from same array of user defaults.


I am using UICollectionView for the same.

Accepted Reply

As documentation says, it is normally not needed to synchronise:

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.


Why do you want to use synchronize and not just save on a file ?

When you are ready to quit, call a save func like this to save myArray:


    func beforeQuit() {

        let defaults = UserDefaults.standard
        defaults.set(myArray, forKey: plistMyArrayKey)
        defaults.synchronize()  // this will sync with settings for App in IOS settings
    }


In appDidFinishLaunching, I register defaults:


        let defaults = [plistMyArrayKey: myArray] as [String : Any]
        UserDefaults.standard.register(defaults: defaults)


To read defaults when loading a viewController:


    override func viewDidLoad() {
     
        super.viewDidLoad()

        // 1. Read preferences
        let defaults = UserDefaults.standard
        myArray = defaults.integer(forKey: plistMyArrayKey)
     }

Replies

As documentation says, it is normally not needed to synchronise:

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.


Why do you want to use synchronize and not just save on a file ?

When you are ready to quit, call a save func like this to save myArray:


    func beforeQuit() {

        let defaults = UserDefaults.standard
        defaults.set(myArray, forKey: plistMyArrayKey)
        defaults.synchronize()  // this will sync with settings for App in IOS settings
    }


In appDidFinishLaunching, I register defaults:


        let defaults = [plistMyArrayKey: myArray] as [String : Any]
        UserDefaults.standard.register(defaults: defaults)


To read defaults when loading a viewController:


    override func viewDidLoad() {
     
        super.viewDidLoad()

        // 1. Read preferences
        let defaults = UserDefaults.standard
        myArray = defaults.integer(forKey: plistMyArrayKey)
     }

(I am not sure what you mean by the 'using append method' - but reduce the problem to storing and recovering an array of strings - you can to the 'append method' to that array.)


It is quite simple. There is no need to synchronize anything. Synshronization is necessary only if you expect your app to crash before the system has a chance to synchronize.


Anywhere in your code just have a:

    [[NSUserDefaults standardUserDefaults] setObject:myArrayOfStrings forKey:@"AnyNameYouWantHere"];

You might do that when you change one of those strings or do it in appliactionDidEnterBackground:


Then in application:didFinishLaunchingWithOptions: reset the array with:


myArrayOfStrings=[[NSUserDefaults standardUserDefaults] objectForKey:@"AnyNameYouWantHere"];

//if myArrayOfStrings is a mutableArray then you will need to use this instead:

myArrayOfStrings=[[[NSUserDefaults standardUserDefaults] objectForKey:@"AnyNameYouWantHere"] mutableCopy];



If the app delegate does not have access to myArrayOfStrings then you will need to do this in the init for the calss that has access to myArrayOfStrings, typically viewDidLoad: