我App Delegate
崩溃后收到此错误。
2016-11-17 10:01:34.780美食家[38429:3212774] *终止应用到期 未被捕获的异常' NSInvalidUnarchiveOperationException',原因: ' * - [NSKeyedUnarchiver decodeObjectForKey:]:无法解码对象 class(Photomania.Item)用于键(NS.objects);可以定义该类 在源代码或未链接的库中
我该如何解决?
这是我的App Delegate
:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// Override point for customization after application launch.
//Create an ImageStore
//let imageStore = ImageStore()
//let itemStore = ItemStore()
//Access the ItemsViewController and set its item store
//let navController = window!.rootViewController as! UINavigationController
//let itemsController = navController.topViewController as! ItemsViewController
//itemsController.itemStore = itemStore
//itemsController.imageStore = imageStore
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let rootController = window?.rootViewController
if rootController is UITabBarController {
let firstTabItem = (rootController as! UITabBarController).viewControllers?[0]
if firstTabItem is UINavigationController {
let firstController = (firstTabItem as! UINavigationController).viewControllers.first as! ItemsViewController
firstController.itemStore = ItemStore()
firstController.imageStore = ImageStore()
}
}
return true
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
}
这是Photomania.Item.swift:
import UIKit
class Item: NSObject, NSCoding {
var meal: String
var restaurantName: String?
var valueInDollars: Int
let dateCreated: NSDate
let itemKey: String
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(meal, forKey: "meal")
aCoder.encodeObject(dateCreated, forKey: "dateCreated")
aCoder.encodeObject(itemKey, forKey: "itemKey")
aCoder.encodeObject(restaurantName, forKey: "restaurantName")
aCoder.encodeInteger(valueInDollars, forKey: "valueInDollars")
}
required init(coder aDecoder: NSCoder) {
meal = aDecoder.decodeObjectForKey("meal") as! String
dateCreated = aDecoder.decodeObjectForKey("dateCreated") as! NSDate
itemKey = aDecoder.decodeObjectForKey("itemKey") as! String
restaurantName = aDecoder.decodeObjectForKey("restaurantName") as! String?
valueInDollars = aDecoder.decodeIntegerForKey("valueInDollars")
super.init()
}
init(meal: String, restaurantName: String, valueInDollars: Int) {
self.meal = meal
self.restaurantName = restaurantName
self.valueInDollars = valueInDollars
self.dateCreated = NSDate()
self.itemKey = NSUUID().UUIDString
super.init()
}
convenience init(random: Bool = false) {
self.init(meal: "Meal", restaurantName: "Restaurant", valueInDollars: 0)
}
}
商品店:
import UIKit
class ItemStore {
var allItems = [Item]()
let itemArchiveURL: NSURL = {
let documentsDirectories =
NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentDirectory = documentsDirectories.first!
return documentDirectory.URLByAppendingPathComponent("items.archive")
} ()
func createItem() -> Item {
let newItem = Item(random: true)
allItems.append(newItem)
return newItem
}
func removeItem(item: Item) {
if let index = allItems.indexOf(item) {
allItems.removeAtIndex(index)
}
}
func moveItemAtIndex(fromIndex: Int, toIndex: Int) {
if fromIndex == toIndex {
return
}
let movedItem = allItems[fromIndex]
allItems.removeAtIndex(fromIndex)
allItems.insert(movedItem, atIndex: toIndex)
}
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL.path!)")
return NSKeyedArchiver.archiveRootObject(allItems, toFile: itemArchiveURL.path!)
}
init() {
if let archivedItems =
NSKeyedUnarchiver.unarchiveObjectWithFile(itemArchiveURL.path!) as? [Item] {
allItems += archivedItems
}
}
func imageURLForKey(key: String) -> NSURL {
let documentsDirectories = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentDirectory = documentsDirectories.first!
return documentDirectory.URLByAppendingPathComponent(key)
}
}