我的对象有以下实现
class CategoriesResponse : NSObject{
let STATUS = "status";
let DATA = "data";
var status : Bool;
var categories : Array<CategoryResponse>;
override init () {
status = false;
categories = Array();
}
init(data : NSDictionary){
status = data[STATUS] as! Bool;
if (status == true) {
if let categoriesArray = data[DATA] as? Array<NSDictionary> {
categories = Array();
for anItem in categoriesArray {
let item: CategoryResponse = CategoryResponse.init(data: anItem);
categories.append(item);
}
} else {
categories = Array();
}
} else {
categories = Array();
}
}
}
和
class CategoryResponse {
let CAT_ID = "CatId";
let NAME = "Name";
let COLOR = "Color";
let CAT_ICON = "CatIcon";
var catId : Int
var name : String;
var color : String;
var catIcon : String;
init(data : NSDictionary){
catId = data[CAT_ID] as! Int;
name = data[NAME] as! String;
color = data[COLOR] as! String;
catIcon = data[CAT_ICON] as! String;
}
func printObject () {
print("----------------------------- Start Print ---------------------------")
print("catId: " + String(catId));
print("name: " + name);
print("color: " + color);
print("catIcon: " + catIcon);
print("------------------------------- End Print ---------------------------")
}
}
我正在尝试使用以下代码
在用户默认值中保存CategoriesResponse类型的对象let defaults = NSUserDefaults.standardUserDefaults()
let dataExample : NSData = NSKeyedArchiver.archivedDataWithRootObject(categories.categories)
defaults.setObject(dataExample, forKey: CATEGORIES);
但是当它运行时它不起作用并且通过异常
任何人都可以告诉我这是什么问题吗?