nskeyedunarchiver

Added a custom framework, now Swift can't unarchive data

痴心易碎 提交于 2019-12-04 03:13:40
问题 I have a pretty trivial Swift app that has a model class named DemoNote . An array of DemoNote instances is read/written via keyed archiving. This worked fine while DemoNote was included in the app. But then I moved DemoNote.swift to a new custom framework called DemoSharedCode . Aside from making sure Xcode was using the framework in the app target, I made sure to Mark DemoNote and its vars and methods as public so they'd be visible outside of the framework Add import DemoSharedCode to any

NSKeyedUnarchiver - try/catch needed?

旧时模样 提交于 2019-12-03 11:06:22
问题 As I understand, the use of @try/@catch blocks is discouraged, because exceptions should only be thrown at unrecoverable, catastrophic errors (refer to this discussion with a nice answer by @bbum: Exception Handeling in iOS). So I looked through my code and found a @try/@catch block that I don't know how to get rid of: NSData *fileData = [NSData dataWithContentsOfFile: ....]; NSDictionary *dictionary; @try { dictionary = [NSKeyedUnarchiver unarchiveObjectWithData: fileData]; } @catch

NSKeyedUnarchiver - try/catch needed?

狂风中的少年 提交于 2019-12-03 02:36:51
As I understand, the use of @try/@catch blocks is discouraged, because exceptions should only be thrown at unrecoverable, catastrophic errors (refer to this discussion with a nice answer by @bbum : Exception Handeling in iOS ). So I looked through my code and found a @try/@catch block that I don't know how to get rid of: NSData *fileData = [NSData dataWithContentsOfFile: ....]; NSDictionary *dictionary; @try { dictionary = [NSKeyedUnarchiver unarchiveObjectWithData: fileData]; } @catch (NSException *exception) { //.... } @finally { //... } The problem is that (as stated in the documentation )

Added a custom framework, now Swift can't unarchive data

家住魔仙堡 提交于 2019-12-01 16:40:49
I have a pretty trivial Swift app that has a model class named DemoNote . An array of DemoNote instances is read/written via keyed archiving. This worked fine while DemoNote was included in the app. But then I moved DemoNote.swift to a new custom framework called DemoSharedCode . Aside from making sure Xcode was using the framework in the app target, I made sure to Mark DemoNote and its vars and methods as public so they'd be visible outside of the framework Add import DemoSharedCode to any classes that want to use DemoNote So now the compiler is happy. But at run time the unarchiving fails

WatchKit NSUserDefaults and NSKeyedUnarchiver issue

孤街浪徒 提交于 2019-12-01 11:21:50
问题 In my project I have a custom object called Country, which implements NSCoding The code is shared across iphone and watch app by reference. Both app are in the same app group. In my iPhone app I'm using NSUserDefaults to story a Country object, from watch app I'm reading the same object with following code: var defaults = NSUserDefaults(suiteName: "group.my.group") if let data = defaults?.objectForKey("country") as? NSData { if let country = NSKeyedUnarchiver.unarchiveObjectWithData(data) as?

Swift only way to prevent NSKeyedUnarchiver.decodeObject crash?

家住魔仙堡 提交于 2019-11-28 19:09:38
NSKeyedUnarchiver.decodeObject will cause a crash / SIGABRT if the original class is unknown. The only solution I have seen to catching this issue dates from Swift's early history and required using Objective C (also pre-dated Swift 2's implementation of guard , throws , try & catch ). I could figure out the Objective C route - but I would prefer to understand a Swift-only solution if possible. For example - the data has been encoded with NSPropertyListFormat.XMLFormat_v1_0 . The following code will fail at unarchiver.decodeObject() if the class of the encoded data is unknown. //... let dat =

NSKeyedUnarchiver error after renaming Xcode project

霸气de小男生 提交于 2019-11-28 03:49:36
问题 I just renamed my Xcode project and when I ran it I got this error: 2015-11-14 05:32:42.337 Buck Tracker[3537:1456100] * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (iBudgeter.Record) for key (NS.objects); the class may be defined in source code or a library that is not linked' The Buck Tracker is the new name and iBudgeter is the original name. Record is a custom NSObject

Unarchive Array with NSKeyedUnarchiver unarchivedObject(ofClass:from:)

情到浓时终转凉″ 提交于 2019-11-27 23:09:09
Since upgrading to Swift 4.2 I've found that many of the NSKeyedUnarchiver and NSKeyedArchiver methods have been deprecated and we must now use the type method static func unarchivedObject<DecodedObjectType>(ofClass: DecodedObjectType.Type, from: Data) -> DecodedObjectType? to unarchive data. I have managed to successfully archive an Array of my bespoke class WidgetData, which is an NSObject subclass: private static func archiveWidgetDataArray(widgetDataArray : [WidgetData]) -> NSData { guard let data = try? NSKeyedArchiver.archivedData(withRootObject: widgetDataArray as Array,

Unarchive Array with NSKeyedUnarchiver unarchivedObject(ofClass:from:)

安稳与你 提交于 2019-11-26 23:08:56
问题 Since upgrading to Swift 4.2 I've found that many of the NSKeyedUnarchiver and NSKeyedArchiver methods have been deprecated and we must now use the type method static func unarchivedObject<DecodedObjectType>(ofClass: DecodedObjectType.Type, from: Data) -> DecodedObjectType? to unarchive data. I have managed to successfully archive an Array of my bespoke class WidgetData, which is an NSObject subclass: private static func archiveWidgetDataArray(widgetDataArray : [WidgetData]) -> NSData { guard