简体   繁体   中英

How to persist data that is appended to a global array swift 2

Before I start, just wanted to say I'm very new to app development in general, I've only been at this for a month, so feel free to dumb it down to me as much as possible haha.

Ok, so I'm working on a quote app and so I've created an array that I can access from any view controller. This that will contain "liked" quotes, which is added from another another view.

Here is my global "likedArray". It resides in its own swift file.

import Foundation

struct Globals {

    static var likedArray: [String] = ["Touch 'Liked' To Continue..."]

}

Quotes are added to likedArray by this method, from another view controller file.

@IBAction func likedbuttonPressed(sender: AnyObject) {

    Globals.likedArray.append(quoteLabel.text)
}

And "liked" quotes are shown in another view via this method

// Like button method to move forward in array
@IBAction func likedButtonTouched(sender: AnyObject) {

    self.favouritesLabel.fadeOut(completion: {
        (finished: Bool) -> Void in

    self.currentlikedArrayIndex++
    if self.currentlikedArrayIndex < Globals.likedArray.count {
        self.favouritesLabel.text = Globals.likedArray[self.currentlikedArrayIndex]
    } else {
        self.currentlikedArrayIndex--
    }

     self.favouritesLabel.fadeIn()

    })  
}

And this all works fine, except that when I quit the app, all liked quotes are gone.

So, my question is how do I save this data? If you need any more information about my code, please let me know. Thanks in advance.

The most hassle free way is probably to use NSUserDefaults , can follow this tutorial to find out how exactly

the jist of the tutorial:

//for writing to storage
let defaults = NSUserDefaults.standardUserDefaults()
let array = ["Hello", "World"]
defaults.setObject(array, forKey: "SavedArray")

//for reading
let array = defaults.objectForKey("SavedArray") as? [String] ?? [String]()

So basically, you should make a setter method for your global array so each time the array is set, it will write to the NSUserDefaults, then on app launch it should populate the array with whats in the NSUserDefaults

update: (just did this off the top of my head)

struct Globals {

    static var likedArray: [String] = ["Touch 'Liked' To Continue..."]

    func addLikedString(likedString: String) {

        self.likedArray.append(likedString)
        NSUserDefaults.standardUserDefaults().setObject(self.likedArray, forKey: "SavedArray")
    }

    func getLikedStringAtIndex(index:Int) -> Int {
        return self.likedArray[index] 
    }

}

//inside your app delegate, or somewhere appropriate, to load the array when the app starts

Globals.likedArray = NSUserDefaults.standardUserDefaults().objectForKey("SavedArray") as? [String] ?? [String]()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM