nskeyedarchiver

NSKeyedUnarchiver.unarchiveTopLevelObjectWithData return nil

房东的猫 提交于 2021-02-11 15:14:16
问题 I want to save an array of objects into UserDefaults and load it back. When trying to unarchive the data it always returns nil.. any idea? This is my object: class DbInsideLevel: NSObject, NSSecureCoding { static var supportsSecureCoding: Bool { return true } let idNum: Int! var topicId: Int = 0 var tryCount: Int = 0 var score: Int = 0 var isOpen: Bool = false var lastPlayedDate: Date? init(idNum: Int, topicId: Int, tryCount: Int = 0, score: Int = 0, open: Bool, lastPlayedDate: Date?) { self

NSKeyedUnarchiver decodeObjectForKey: cannot decode object of class for key (NS.objects)

[亡魂溺海] 提交于 2021-02-10 07:06:57
问题 I looked through whole SO but still no answer. My app reports this problem: Fatal Exception: NSInvalidUnarchiveOperationException *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (App_Title.Products) for key (NS.objects); the class may be defined in source code or a library that is not linked I did already do NSKeyedUnarchiver.setClass just before #unarchiveObject: func loadProducts() -> [Products]? { NSKeyedUnarchiver.setClass(Products.self, forClassName: "Products

iOS 12.0 Alternative to Using Deprecated archiveRootObject:toFile:

旧巷老猫 提交于 2020-12-29 05:24:27
问题 With iOS 12, archiveRootObject:toFile:has been deprecated. Can anyone suggest a streamlined alternative to archiving objects to a file? //Generic example of archiver prior to iOS 12.0 -(BOOL) archive:(id)archiveObject withFileName:(NSString*)filename { NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; return [NSKeyedArchiver archiveRootObject:archiveObject toFile:path]; } 回答1: Thanks to @vadian for the hint, here's what I've come up with to do archiving and

iOS 12.0 Alternative to Using Deprecated archiveRootObject:toFile:

廉价感情. 提交于 2020-12-29 05:24:11
问题 With iOS 12, archiveRootObject:toFile:has been deprecated. Can anyone suggest a streamlined alternative to archiving objects to a file? //Generic example of archiver prior to iOS 12.0 -(BOOL) archive:(id)archiveObject withFileName:(NSString*)filename { NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; return [NSKeyedArchiver archiveRootObject:archiveObject toFile:path]; } 回答1: Thanks to @vadian for the hint, here's what I've come up with to do archiving and

Store Custom Objects in NSUserDefaults

放肆的年华 提交于 2020-02-25 23:06:25
问题 I am trying to store a custom objects as follows, but I am getting an error. // store data NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:pOrderElements.count]; for (id orderObject in pOrderElements) { NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:orderObject]; [archiveArray addObject:personEncodedObject]; } NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; [userData setObject:archiveArray forKey:@"personDataArray"]; // retrive

Swift: self.init (coder : aDecoder) is crashing app with EXC_BAD_ACCESS

徘徊边缘 提交于 2020-01-17 06:46:35
问题 Error is crashing app when using NSCoder and NSKeyArchiver. I had made a recent post around NSCoder but since then I've changed my code around and got a new error and decided a new post is best. The app is a blog reader, reading from a MYSQL database using PHP to fill a table view with custom objects in Swift using JSON. I've been trying to save mainArray so that when the user moves cells across sections (each section has an array) it can save where the user left it. Blog.swift: Handles the

NSKeyedArchiver/NSKeyedUnarchiver compatibility on different iOS versions

点点圈 提交于 2020-01-06 04:12:10
问题 In an iOS app, we are using serialization of complex objects through NSKeyedArchiver/NSKeyedUnarchiver to store info and data. So the app will include such a serialized object in the app bundle, and this file will be created under one specific iOS version (let's say iOS 5.0) As the app will be running on different iOS versions (> 5.0) and even in future versions to come, we want to be sure that the original serialized object included in the app bundle will be perfectly readable on every other

NSData from NSKeyedArchiver to NSString

随声附和 提交于 2020-01-01 05:03:27
问题 I'm trying to convert NSData generated from NSKeyedArchiver to an NSString so that I can pass it around and eventually convert it back to NSData. I have to pass this as a string (I'm using three20 URL passing). I've gone through various encodings, UTF8, ASCII, etc. and can't get anything to work. NSKeyedArchiver says that the NSData is formated as a property list: NSPropertyListBinaryFormat_v1_0. Does anyone have any idea how I can convert this NSData to a String and back again? Size of the

Saving an NSArray of custom objects

一世执手 提交于 2019-12-30 11:19:06
问题 I've created a subclass of UIImage (UIImageExtra) as I want to include extra properties and methods. I have an array that contains instances of this custom class.However when I save the array, it appears the extra data in the UIImageExtra class is not saved. UIImageExtra conforms to NSCoding, but neither initWithCoder or encodeWithCoder are called, as NSLog statements I've added aren't printed. My method to save the array looks like this: - (void)saveIllustrations { if (_illustrations == nil)

Using NSCoder and NSKeyedArchiver with runtime reflection to deep copy a custom class

僤鯓⒐⒋嵵緔 提交于 2019-12-25 04:16:05
问题 I'd like to use - (id)initWithCoder:(NSCoder *)coder and - (void)encodeWithCoder:(NSCoder *)coder to encode a custom class for copying (using NSKeyedArchiver etc) My custom class contains a large number of iVars. Instead of listing all of these in the encoding code I'd like to use reflection to simply enumerate over all the properties in the custom class and encode/decode each property from within a loop. I'll end up with code that looks a bit like this: - (id)initWithCoder:(NSCoder *)coder {