SWIFT如何创建NSCoding子类并从另一个类调用它? [英] SWIFT How to create a NSCoding Subclass and call it from another class?
问题描述
我在NSCoding上发现了这个黑色代码,它几乎想要我想要它。我发现它的链接在下面。
如何在其他类中创建NSCoding类和用户?下面的代码不起作用。我希望有些人可以帮助我。
import Foundation
import UIKit
class用户: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)
}
}
//新类我要设置并获取对象
class MyNewClass:UIViewController {
let user = User(name:Mike)
let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user)
let decodingUser = NSKeyedUnarchiver.unarchiveObjectWithData(encodedUser)as User
}
//http://stackoverflow.com/questions/24589933/nskeyedunarchiver-fails-to-decode-a- custom-object-in-swift
import UIKit
import Foundation
class ViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
var instanceData = Data()
instanceData.name =testName
ArchiveData()。saveData(nameData:instanceData)
let retrieveData = ArchiveData()。retrieveData()as Data
println(retrieveData.name)
}
}
类数据:NSObject {
var name:String =
func encodeWithCoder(aCoder:NSCoder!){
aCoder.encodeObject(name,forKey:name)
}
init(coder aDecoder:NSCoder!){
name = aDecoder.decodeObjectForKey(name)as String
}
覆盖init(){
}
}
类ArchiveData :NSObject {
var docume ntDirectories: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(写入文件的成功!)
} else {
println(无法写入文件!)
}
}
func retrieveData() - > NSObject {
var dataToRetrieve = Data()
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)
documentDirectory = documentDirectories.objectAtIndex(0)as String
path = documentDirectory。 stringByAppendingPathComponent(data.archive)
如果让dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path)为?数据{
dataToRetrieve = dataToRetrieve2 as Data
}
return(dataToRetrieve)
}
}
I found this black of code on NSCoding and it almost does want I want it to. the link for where I found it is below. How do I create a NSCoding class and user in in other classes? The below code dies not work. I hope some can help me with this.
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
I'm cutting and pasting from my own project below. I have limited this to one string parameter to store to file. But you can more of different types. You can paste this into a single swift file and use it as the ViewController plus added classes to test. It demonstrates using NSCoding with swift syntax to save and retrieve data in an object.
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)
}
}
这篇关于SWIFT如何创建NSCoding子类并从另一个类调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!