swift - iOS App Delegate Crash -
i receiving error after app delegate
crashes.
2016-11-17 10:01:34.780 foodie[38429:3212774] * terminating app due uncaught exception 'nsinvalidunarchiveoperationexception', reason: '* -[nskeyedunarchiver decodeobjectforkey:]: cannot decode object of class (photomania.item) key (ns.objects); class may defined in source code or library not linked'
how can fix it?
here app delegate
:
@uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? // override point customization after application launch. //create imagestore //let imagestore = imagestore() //let itemstore = itemstore() //access itemsviewcontroller , set 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 uitabbarcontroller { let firsttabitem = (rootcontroller as! uitabbarcontroller).viewcontrollers?[0] if firsttabitem 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) { } }
here 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) } }
item store:
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) } }
Comments
Post a Comment