How can I store an array of custom objects (Goals)

后端 未结 3 905
情书的邮戳
情书的邮戳 2021-01-19 18:49

How can I store an array of objects of type Goal which I have created in NSUserDefaults? (in swift)

Here is the code:

func saveGoalList ( newGoalList         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 19:22

    I am posting code from a learning project I did to store objects using NSCoding. Fully functional and ready to use. A math game that was storing game variables, etc.

    //********This class creates the object and properties to store********
    import Foundation
    class ButtonStates: NSObject {
    
        var sign: String = "+"
        var level: Int = 1
        var problems: Int = 10
        var time: Int = 30
        var skipWrongAnswers = true
    
        func encodeWithCoder(aCoder: NSCoder!) {
            aCoder.encodeObject(sign, forKey: "sign")
            aCoder.encodeInteger(level, forKey: "level")
            aCoder.encodeInteger(problems, forKey: "problems")
            aCoder.encodeInteger(time, forKey: "time")
            aCoder.encodeBool(skipWrongAnswers, forKey: "skipWrongAnswers")
        }
    
        init(coder aDecoder: NSCoder!) {
            sign = aDecoder.decodeObjectForKey("sign") as String
            level = aDecoder.decodeIntegerForKey("level")
            problems = aDecoder.decodeIntegerForKey("problems")
            time = aDecoder.decodeIntegerForKey("time")
            skipWrongAnswers = aDecoder.decodeBoolForKey("skipWrongAnswers")
        }
    
        override init() {
        }
    }
    
    
    
    
       //********Here is the data archiving and retrieving class********
        class ArchiveButtonStates:NSObject {
    
            var documentDirectories:NSArray = []
            var documentDirectory:String = ""
            var path:String = ""
    
            func ArchiveButtons(#buttonStates: ButtonStates) {
                documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                documentDirectory = documentDirectories.objectAtIndex(0) as String
                path = documentDirectory.stringByAppendingPathComponent("buttonStates.archive")
    
                if NSKeyedArchiver.archiveRootObject(buttonStates, toFile: path) {
                    //println("Success writing to file!")
                } else {
                    println("Unable to write to file!")
                }
            }
    
            func RetrieveButtons() -> NSObject {
                var dataToRetrieve = ButtonStates()
                documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                documentDirectory = documentDirectories.objectAtIndex(0) as String
                path = documentDirectory.stringByAppendingPathComponent("buttonStates.archive")
                if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? ButtonStates {
                    dataToRetrieve = dataToRetrieve2 as ButtonStates
                }
                return(dataToRetrieve)
            }
        }
    
    
    the following is in my ViewController where the game is played.  Only showing the relevant code for retrieving and storing objects
    
    class mathGame: UIViewController {
    
    var buttonStates = ButtonStates()
    
    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            //set inital view
    
            //retrieving a stored object & placing property into local class variables
            buttonStates = ArchiveButtonStates().RetrieveButtons() as ButtonStates
            gameData.sign = buttonStates.sign
            gameData.level = buttonStates.level
            gameData.problems = buttonStates.problems
            gameData.time = buttonStates.time
    
        }
    
    override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)
    
          //storing the object
          ArchiveButtonStates().ArchiveButtons(buttonStates: buttonStates)
        }
    }
    

提交回复
热议问题