I have a custom class with an object pertaining to that class:
import UIKit;
var managerObj = Manager() //Instance of an FUT class
/*************************************************************
*
* Class: Manager
* Description: This class handles all of the data
* in the application. It also stores the
* information.
*
**************************************************************/
class Manager {
// Get the current time when the user starts a ride.
var beginTime = NSCalendar.currentCalendar().components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: NSDate())
// Get the HTML/CSS/JavaScript template path for read/write
let coverTemplateFilePath = NSBundle.mainBundle().pathForResource("CoverTemplate", ofType: "txt")
let issueTemplateFilePath = NSBundle.mainBundle().pathForResource("Template", ofType: "txt")
var Mgr = RideManager() //Instance of a RideManager class
var voiceRec = SKRecognizer()
var issueEditFlag : Bool = false //Flag if user is trying to edit issue
var issueBeingEdited = 0 //The item in the array user is trying to edit
//Sets the values for the ride
func setRideVariables(Ride:String, Tester:String, Vehicle:String, SW:String, HW:String, startTime:String, startMiles:String, Date:String){
//set object values here...
}
}
After I add the fields for that object I need to figure out a way to save that information so that if the program exits, the information can be easily loaded. I try to save the futReport
object into NSUserDefaults in the AppDelegates.swift file like so:
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
var appDefault = NSUserDefaults.standardUserDefaults()
appDefault.setValue(managerObj, forKey: "managerObj")
appDefault.synchronize()
}
Then I try to load the object information once the application becomes active again like so:
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
var appDefault = NSUserDefaults.standardUserDefaults()
if (appDefault.valueForKey("managerObj") != nil) {
managerObj = appDefault.valueForKey("managerObj") as Manager!
}
}
But it keeps erroring out at managerObj = appDefault.valueForKey("managerObj") as Manager!
I'm assuming it is because the valueForKey()
method is support to return a string or integer and not an object from class FUTManager. Does anyone have any ideas?
I tried searching for someone who had a similar issue but I couldn't find a solution.
Thank you!
EDIT: I'm not familiar with Objective-C at all. If you could please explain it in Swift, that would be great!!