我在Swift中使用NSCoding
时遇到问题。我试图实现协议,但每当我调用save()
方法时,我都会崩溃。 Xcode将我拉到encodeWithCoder
的线上。这是我的代码,任何建议都会随之而来。
class CourseList: NSObject, NSCoding {
...
init(coder aDecoder: NSCoder!){
super.init()
if aDecoder != nil {
self.courses = aDecoder.decodeObjectForKey("courses") as Dictionary<String,course>
}
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder!.encodeObject(self.courses, forKey: "courses") // EXC_BAD_INSTRUCTOIN
}
}
我写了save()
和load()
来调用ViewController
。
func save() {
var archivedObject = NSKeyedArchiver.archivedDataWithRootObject(History) //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
var defaults = NSUserDefaults.standardUserDefaults() //[defaults setObject:archivedObject forKey:<key_for_archived_object>];
defaults.setObject(History.courses, forKey: "courses")
defaults.synchronize()
}
func load() {
var defaults = NSUserDefaults.standardUserDefaults() // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
var archivedObject = defaults.objectForKey("courses") as NSData // NSData *archivedObject = [defaults objectForKey:<key_for_archived_object>];
History = NSKeyedUnarchiver.unarchiveObjectWithData(archivedObject) as Dictionary<String,course> //<your_class> *obj = (<your_class> *)[NSKeyedUnarchiver unarchiveObjectWithData:archivedObject];
}
答案 0 :(得分:37)
自从提出这个问题以来,Swift已经发生了一些变化。我无法重现您所看到的错误,但我能够使用下面的代码使用Swift 1.0来使用NSCoding。
class CourseList: NSObject, NSCoding
{
var myCourses: Dictionary<String, String>?
override init() {}
required init(coder aDecoder: NSCoder) {
self.myCourses = aDecoder.decodeObjectForKey("myCourses") as? Dictionary
}
func encodeWithCoder(aCoder: NSCoder) {
if let courses = self.myCourses{
aCoder.encodeObject(courses, forKey: "myCourses")
}
}
func populateCourses() {
self.myCourses = ["cs101": "Hello World"]
}
func save() {
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "courseList")
}
func clear() {
NSUserDefaults.standardUserDefaults().removeObjectForKey("courseList")
}
class func loadSaved() -> CourseList? {
if let data = NSUserDefaults.standardUserDefaults().objectForKey("courseList") as? NSData {
return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? CourseList
}
return nil
}
}
加载已保存的实例或根据需要创建新实例
// Try loading a saved version first
if let courseLs = CourseList.loadSaved() {
println("loaded Save CourseList")
} else {
// Create a new Course List
let courseLs: CourseList = CourseList()
courseLs.populateCourses()
courseLs.save()
}
答案 1 :(得分:15)
class Student: NSObject, NSCoding {
var name: String!
var className: String!
var rollNo: Int!
var marks: [Float]!
// MARK: NSCoding
required convenience init(coder decoder: NSCoder) {
self.init()
self.name = decoder.decodeObjectForKey("name") as String?
self.className = decoder.decodeObjectForKey("className") as String?
self.rollNo = decoder.decodeIntegerForKey("rollNo")
self.marks = decoder.decodeObjectForKey("marks") as [Float]!
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
coder.encodeObject(self.className, forKey: "className")
coder.encodeInt(Int32(self.rollNo), forKey: "rollNo")
coder.encodeObject(self.marks, forKey: "marks")
}
}
上面的类字符串,实现了整数,数组编码。
假设学生是班级学生的实例
<强>归档强>
文件系统:
NSKeyedArchiver.archiveRootObject(student, toFile: "Here give your file path")
NSUserDefaults:
let data = NSKeyedArchiver.archivedDataWithRootObject(student)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "student")
<强>取消存档强>
文件系统:
NSKeyedUnarchiver.unarchiveObjectWithFile(student, toFile: "Here give your file path")
NSUserDefaults:
if let data = NSUserDefaults.standardUserDefaults().objectForKey("student") as? NSData {
let student = NSKeyedUnarchiver.unarchiveObjectWithData(data)
}
<强> ADDIT:强>
保存/阅读的文件路径
func saveFileLocation() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths[0] as! String
return documentDirectory.stringByAppendingPathComponent("savefile")
}
答案 2 :(得分:4)
以下是另一个名为SNStock
的对象的示例,它有两个字符串属性,ticker
和name
:
import Foundation
class SNStock: NSObject, NSCoding {
let ticker: NSString
let name: NSString
init(ticker: NSString, name: NSString) {
self.ticker = ticker
self.name = name
}
// MARK: NSCoding
required init(coder aDecoder: NSCoder) {
self.ticker = aDecoder.decodeObjectForKey("ticker") as! NSString
self.name = aDecoder.decodeObjectForKey("name") as! NSString
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(ticker, forKey: "ticker")
aCoder.encodeObject(name, forKey: "name")
}
// MARK: NSObjectProtocol
override func isEqual(object: AnyObject?) -> Bool {
if let object = object as? SNStock {
return self.ticker == object.ticker &&
self.name == object.name
} else {
return false
}
}
override var hash: Int {
return ticker.hashValue
}
}
答案 3 :(得分:0)
以下代码不起作用
class Hero : AnyObject, NSCoding {
var heroID: String?
var title: String?
var imageName: String?
var detail: String?
var star: String?
init() {
}
func update(_ dictionary: Dictionary<String, AnyObject>) {
heroID = dictionary["imageName"] as? String
title = dictionary["title"] as? String
imageName = dictionary["imageName"] as? String
detail = dictionary["detail"] as? String
star = dictionary["star"] as? String
}
required init?(coder aDecoder: NSCoder) {
heroID = aDecoder.decodeObject(forKey: "heroID") as? String
title = aDecoder.decodeObject(forKey: "title") as? String
imageName = aDecoder.decodeObject(forKey: "imageName") as? String
detail = aDecoder.decodeObject(forKey: "detail") as? String
star = aDecoder.decodeObject(forKey: "star") as? String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(heroID, forKey: "heroID")
aCoder.encode(title, forKey: "title")
aCoder.encode(imageName, forKey: "imageName")
aCoder.encode(detail, forKey: "detail")
aCoder.encode(star, forKey: "star")
}
}
一定要这样写
class Hero : NSObject, NSCoding {
var heroID: String?
var title: String?
var imageName: String?
var detail: String?
var star: String?
override init() {
}
}