0

Hi I have a problem trying to save my data to a plist, my class looks like this:

struct timerData {

var timerCount: NSTimeInterval
var timerText: String
var timerRunning: Bool
var soundSwitch: Bool
var vibrateSwitch: Bool
var alertInfo: [alertData?]

//initialisation
init(timerCounter: Double, timerText: String, soundSwitch: Bool, vibrateSwitch: Bool, alertInfo: [alertData?]) {
    self.timerCount = timerCounter
    self.timerRunning = false
    self.timerText = timerText
    self.soundSwitch = soundSwitch
    self.vibrateSwitch = vibrateSwitch
    self.alertInfo = alertInfo
}
}

I've figured out how to save each part individually to arrays in the plist, but i realised it would be much easier to save an array of the timerData, since it's already set up.

My plist file directory is coming from the app delegate as i'm using tab views

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let pathForThePlistFile = appDelegate.plistPathInDocument

            //This is the data structure
            let namesArray = NSMutableDictionary(contentsOfFile: pathForThePlistFile)

            //creating array for the data to be passed in
            var timerDataArray = [timerData]()

           //initialise values to that struct
            var timerDataTest = timerData(timerCounter: self.timerCounter.countDownDuration, timerText: (self.promptText?.text)!, soundSwitch: self.soundSwitch.on, vibrateSwitch: self.vibrateSwitch.on, alertInfo: self.alertDataSource)

            //append to array
            timerDataArray.append(timerDataTest)
            print(timerDataArray)

            //OPTION 1: found this on another stack thread **This fails
            NSKeyedArchiver.archiveRootObject(timerDataArray as! AnyObject, toFile: pathForThePlistFile)

           //OPTION 2: set the value to an array in the plist ** This also fails because of type
            namesArray!.setValue(timerDataArray as? AnyObject, forKey: "data1")

            print(namesArray)

            // Save to plist
            namesArray!.writeToFile(pathForThePlistFile, atomically: true)

I know it's an issue with my array type, but i'm not sure how to cast it to a type that will be accepted in the plist, i've looked at loads of already answered questions and tried loads but I can't figure it out. Thanks in advance for any help

Ali_bean
  • 341
  • 3
  • 14
  • I actually have the same question when trying to save them individually for the case of alertDataSource which is an array of custom structs – Ali_bean Nov 09 '15 at 23:19
  • I would start by unwrapping your optionals. I refuse to debug code with that many force unwrapped optionals. – R Menke Nov 09 '15 at 23:42
  • I'm pretty new to swift so i normally let xcode sort out the optionals, if it suggests to put a bang i do, i know that's really bad, sorry – Ali_bean Nov 09 '15 at 23:45
  • Xcode does not sort out optionals... at all.... not even a little bit.... It suggests `!` as a way to silence the Xcode warning. It however has no effect on your actual code. It will not magically make something work. – R Menke Nov 09 '15 at 23:47
  • Ok, thanks for your input, i have done a course on optionals but i will look into it more – Ali_bean Nov 09 '15 at 23:49
  • loop up optional binding, for example [here](http://stackoverflow.com/questions/24128860/how-is-optional-binding-used-in-swift) Ideally you never use `!` and you only use `?` at declaration and down-casting. – R Menke Nov 09 '15 at 23:54

0 Answers0