簡體   English   中英

SWIFT如何創建NSCoding子類並從另一個類中調用它?

[英]SWIFT How to create a NSCoding Subclass and call it from another class?

我在NSCoding上發現了這黑碼,幾乎想要我這樣做。 我找到它的鏈接在下面。 如何在其他類中創建NSCoding類和用戶? 下面的代碼死了不起作用。 我希望有人可以幫助我。

import Foundation
import UIKit


class User: NSObject, NSCoding {
    var name: String

    init(name: String) {
        self.name = name
    }

   required init(coder aDecoder: NSCoder) {

        self.name = aDecoder.decodeObjectForKey("name") as String
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(name, forKey: "name")
    }
}


//new class where I want to set and get the object

class MyNewClass: UIViewController {

    let user = User(name: "Mike")
    let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user)
    let decodedUser = NSKeyedUnarchiver.unarchiveObjectWithData(encodedUser) as User

}

//http://stackoverflow.com/questions/24589933/nskeyedunarchiver-fails-to-decode-a-custom-object-in-swift

我正在從下面的我自己的項目中剪切和粘貼。 我將其限制為一個字符串參數以存儲到文件。 但是您可以使用更多不同的類型。 您可以將其粘貼到單個swift文件中,並將其用作ViewController以及添加的類進行測試。 它演示了將NSCoding與swift語法一起使用來保存和檢索對象中的數據。

import UIKit
import Foundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var instanceData = Data()
        instanceData.name = "testName"
        ArchiveData().saveData(nameData: instanceData)
        let retrievedData = ArchiveData().retrieveData() as Data
        println(retrievedData.name)

    }
}

class Data: NSObject {

    var name: String = ""

    func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeObject(name, forKey: "name")
    }

    init(coder aDecoder: NSCoder!) {
        name = aDecoder.decodeObjectForKey("name") as String
    }

    override init() {
    }
}

class ArchiveData:NSObject {

    var documentDirectories:NSArray = []
    var documentDirectory:String = ""
    var path:String = ""

    func saveData(#nameData: Data) {
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("data.archive")

        if NSKeyedArchiver.archiveRootObject(nameData, toFile: path) {
            //println("Success writing to file!")
        } else {
            println("Unable to write to file!")
        }
    }

    func retrieveData() -> NSObject {
        var dataToRetrieve = Data()
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("data.archive")
        if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? Data {
            dataToRetrieve = dataToRetrieve2 as Data
        }
        return(dataToRetrieve)
    }
}

暫無
暫無

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

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