Questions tagged [nskeyedarchiver]

NSKeyedArchiver, a concrete subclass of NSCoder, provides a way to encode objects (and scalar values) into an architecture-independent format that can be stored in a file. When you archive a set of objects, the class information and instance variables for each object are written to the archive. NSKeyedArchiver’s companion class, NSKeyedUnarchiver, decodes the data in an archive and creates a set of objects equivalent to the original set.

NSKeyedArchiver, a concrete subclass of NSCoder, provides a way to encode objects (and scalar values) into an architecture-independent format that can be stored in a file. When you archive a set of objects, the class information and instance variables for each object are written to the archive. NSKeyedArchiver’s companion class, NSKeyedUnarchiver, decodes the data in an archive and creates a set of objects equivalent to the original set.

A keyed archive differs from a non-keyed archive in that all the objects and values encoded into the archive are given names, or keys. When decoding a non-keyed archive, values have to be decoded in the same order in which they were encoded. When decoding a keyed archive, because values are requested by name, values can be decoded out of sequence or not at all. Keyed archives, therefore, provide better support for forward and backward compatibility.

Click Here for class reference

570 questions
2
votes
1 answer

How can I encode an array of simd_float4x4 elements in Swift (convert simd_float4x4 to Data)?

I am building an app that captures facetracking data from the iPhone TrueDepth camera. I need to write this data to files so I can use it as the basis for another app. Within the app, the data is saved into four separate arrays, one containing…
MightyMeta
  • 599
  • 4
  • 14
2
votes
1 answer

NSKeyedUnarchiver issue when moving model to framework

I have moved part of my code into a separate framework, and that framework contains a model used by the app to persist data to CoreData. Now when I try to launch the app, and it tries to restore state from CoreData, I get the following…
2
votes
1 answer

NSKeyedUnarchiver returning nil when retrieving custom class

In my view controller, I have a custom class store in an array. I was using NSKeyedArchiver to store data but I was using the depreciated version NSKeyedUnarchiver.unarchiveObject(withFile: object.ArchiveURL.path) as? [object]. Now I have decided to…
ewcrow
  • 23
  • 4
2
votes
2 answers

Cannot figure out why my app crashes when I use NSKeyedArchivers / NSKeyedUnarchivers

I am developing my first iphone 'Diary' app, which uses custom 'Entry' objects that hold an NSString title, NSString text and NSDate creationDate. When I try to archive an NSMutableArray of Entry objects, and later retrieve them the next time the…
2
votes
1 answer

iOS 13 NSAttributedString NSKeyedArchiver.archivedData lost attributes

I have following extension for NSAttributedString: extension NSAttributedString { func toHtmlString() -> String { let documentAttributes: [NSAttributedString.DocumentAttributeKey : Any] =…
Yuan Fu
  • 310
  • 2
  • 14
2
votes
0 answers

Decoding an object when original class is unavailable and class is unknown

This is very similar to this question: How can I decode an object when original class is not available?, but what's the best way to do this if the data I am unarchiving could be any class conforming to a certain protocol? As a simple example,…
Cody
  • 650
  • 9
  • 16
2
votes
1 answer

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

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…
S. Gissel
  • 1,788
  • 2
  • 15
  • 32
2
votes
2 answers

[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance

I am trying to archive a dictionary using the following code and getting error. Obviously there is something is wrong in the response dictionary I am passing but the trace doesn't tell anything. How do I nail down the root cause? do { let data =…
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
2
votes
1 answer

NSKeyedArchiver: casting Data returning nil - Swift

Well, investigated several similar topics here, done everything as suggested, but my computed property "previousUserData" returns me nil, when trying to cast the decoded object to my type. What's wrong? @objc class PreviousUserData: NSObject,…
Andrew V. Jackson
  • 273
  • 1
  • 4
  • 14
2
votes
2 answers

iOS - encoding/decoding enums - crash on accessing after relaunch

What am I doing wrong? Am I encoding/decoding the enum type properly? GameSettings interface: typedef enum { CatWhite = 0, CatBlack = 1, CatOrange = 2 } CatColor; ... CatColor catColor; ... @property CatColor catColor; GameSettings…
sol
  • 6,402
  • 13
  • 47
  • 57
2
votes
1 answer

NSKeyedArchiver and sharing a custom class between targets

My app uses a custom class as its data model: class Drug: NSObject, NSCoding { // Properties, methods etc... } I have just created a Today extension and need to access the user’s data from it, so I use NSCoding to persist my data in both the…
Chris
  • 4,009
  • 3
  • 21
  • 52
2
votes
1 answer

what file extension do i use with nskeyedarchiver

I am writing an app that uses NSKeyedArchiver to save its data. Should my file be ishotTrack.plist or ishotTrack.arch?
Blane Townsend
  • 2,888
  • 5
  • 41
  • 55
2
votes
2 answers

How do you save data with NSKeyedArchiver?

Hi, I am trying to save an object from a class I have created. It is called shot and Contains 5 variables I wish to save. Here is the .h file--- It cut off NSCoding and NSMutableCopying Protocals and my imports, but they are there. #import…
Blane Townsend
  • 2,888
  • 5
  • 41
  • 55
2
votes
1 answer

NSCoding in Swift 3 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.User encodeWithCoder:]

I have issues to implement NSCoding. Here is my code for User Class: public class User: NSCoder { ... NSCoding methods: Decoder: required public init(coder aDecoder: NSCoder) { self.deviceToken = aDecoder.decodeObject(forKey:…
Josep Escobar
  • 445
  • 4
  • 11
2
votes
1 answer

Save Custom Dictionary to User default

struct AllItemsData { var DSTBID: String! var CCAS: String! var BCAS: String! } This is my structure from which I create an array of type AllItemsDataArray = [AllItemsData()] After adding some data, now I want to store it into user…