簡體   English   中英

如何從NSUserDefaults中的自定義類存儲對象?

[英]How can I store my object from a custom class in NSUserDefaults?

我有一個與該類有關的對象的自定義類:

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...
    }
}

在添加了該對象的字段之后,我需要找出一種方法來保存該信息,以便在程序退出時可以輕松地加載該信息。 我嘗試將futReport對象保存到AppDelegates.swift文件中的NSUserDefaults中,如下所示:

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()
    }

然后,一旦應用程序再次變為活動狀態,我將嘗試加載對象信息,如下所示:

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!
        }
    }

但是,它總是在managerObj = appDefault.valueForKey("managerObj") as Manager!managerObj = appDefault.valueForKey("managerObj") as Manager! 我假設這是因為valueForKey()方法支持返回字符串或整數,而不是來自類FUTManager的對象。 有人有什么想法嗎?

我嘗試搜索存在類似問題的人,但找不到解決方案。

謝謝!

編輯:我根本不熟悉Objective-C。 如果您能在Swift中進行解釋,那太好了!!

存儲對象

var customObject = CustomClass()

let encodedObj = NSKeyedArchiver.archivedDataWithRootObject(customObject)
NSUserDefaults.standardUserDefaults().setObject(encodedObj, forKey: "encodedObjectKey")

檢索存儲的對象,

let encodedObj = NSUserDefaults.standardUserDefaults().objectForKey("encodedObjectKey") as! NSData
var customObject = NSKeyedUnarchiver.unarchiveObjectWithData(encodedObj) as! CustomClass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM