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