【问题标题】:SWIFT How to create a NSCoding Subclass and call it from another class?SWIFT 如何创建一个 NSCoding 子类并从另一个类调用它?
【发布时间】:2014-09-28 16:16:09
【问题描述】:

我在 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

【问题讨论】:

    标签: ios inheritance swift nscoding


    【解决方案1】:

    我正在从下面我自己的项目中剪切和粘贴。我已将此限制为一个要存储到文件的字符串参数。但是你可以更多不同的类型。您可以将其粘贴到单个 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)
        }
    }
    

    【讨论】:

    • 谢谢,这就是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多