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

554 questions
26
votes
7 answers

Save and retrieve value via KeyChain

I'm trying to store an Integer and retrieve it using KeyChain. This is how I save it: func SaveNumberOfImagesTaken() { let key = "IMAGE_TAKEN" var taken = 10 let data = NSKeyedArchiver.archivedDataWithRootObject(taken) …
Roi Mulia
  • 4,846
  • 9
  • 43
  • 92
22
votes
4 answers

Swift structs to NSData and back

I have a struct containing a struct and an NSObject that I want to serialize into an NSData object: struct Packet { var name: String var index: Int var numberOfPackets: Int var data: NSData } var thePacket = Packet(name: name, index: i,…
niklassaers
  • 7,810
  • 18
  • 90
  • 141
19
votes
4 answers

iOS 12.0 Alternative to Using Deprecated archiveRootObject:toFile:

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…
rswayz
  • 892
  • 2
  • 7
  • 20
18
votes
4 answers

How to solve deprecation of unarchiveObject(withFile:)

With iOS 12.1, unarchiveObject(withFile:) was deprecated. How can you convert NSKeyedUnarchiver.unarchiveObject(withFile: String) to use a call to NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data: Data), or…
Diskprotek
  • 481
  • 6
  • 12
18
votes
2 answers

Simple persistent storage in Swift

I have an array of objects each with a number of properties. Here is some sample data taken by looping through the array of objects: Name = Rent Default Value 750 This Months Estimate = 750 Sum Of This Months Actuals = 0 Risk Factor = 0.0 Monthly…
18
votes
3 answers

Encode NSArray or NSDictionary using NSCoder

I was wondering whether or not it is possible to use the NSCoder method: - (void)encodeObject:(id)objv forKey:(NSString *)key to encode either an instance of NSArray or NSDictionary. If not how do you go about encoding them? I am trying to use…
Ron Dear
  • 515
  • 1
  • 5
  • 16
17
votes
5 answers

NSKeyedUnarchiver - delete decoded data?

I couldn't get any replies on my previous (related) question, so I'm wondering if slightly paraphrasing it will be of any help. I'm encoding a few complex objects with NSKeyedArchiver and saving it to disk. Say, something like - Class member { …
Tejaswi Yerukalapudi
  • 8,629
  • 12
  • 57
  • 101
14
votes
4 answers

IOS editor bug. archivedData renamed

Please help me! I am stuck in a loop and can't find my way out. I am trying to learn IOS programming for work so I thought I would start with their tutorial app the Meal list application. I am at the part where you are supposed to start saving…
Neglected Sanity
  • 1,194
  • 1
  • 17
  • 36
14
votes
1 answer

How to replace NSKeyedArchiver's initializer init(forWritingWith:) in iOS 12 to encode the metadata of a CKRecord

In iOS 12, the NSKeyedArchiver's initializer init(forWritingWith:) was deprecated. Xcode 10 recommends replacing it with the new initializer init(requiringSecureCoding:). The problem is that this initializer only sets the value of the…
Mikiko Jane
  • 276
  • 5
  • 11
13
votes
0 answers

Can I get all keys of a NSKeyedUnarchiver?

I want do something with these step: Encode a kind of object A with NSKeyedArchiver M and I can get a NSData X. Init another kind of object B with NSKeyedUnarchiver N from NSData X. The names of A and B are the same, so it's possible(I have done an…
user509027
  • 131
  • 5
12
votes
1 answer

NSCocoaErrorDomain Code=256 File couldn’t be opened

The file were created in old project in Objective-C. NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataForWrite]; [archiver encodeObject:dictVer forKey:@"cityVersionDict"]; [archiver finishEncoding]; BOOL flag =…
sam chi wen
  • 621
  • 6
  • 10
12
votes
3 answers

Cannot decode object of class Employee for key (NS.object.0); the class may be defined in source code or a library that is not linked

Im trying to pass an array of 'Employee' objects iPhone to Apple Watch by serializing the array : NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees]; and unserializing it as on the Watch side: NSMutableArray *employees =…
12
votes
2 answers

How do I save a UIColor with UserDefaults?

I'm trying to program my code so that if the user presses the Night Button the background will turn black and stay black if the user closes the app. (Same goes for day mode.) Please note: I already coded buttons and when they press it, all of the…
10
votes
1 answer

NSArchiver vs. NSKeyedArchiver

How do you know if you need to use NSArchiver or NSKeyedArchiver? What's the difference?
node ninja
  • 28,340
  • 55
  • 153
  • 242
10
votes
4 answers

Saving PFObject NSCoding

My Problem: saveInBackground isn't working. The Reason It's not working: I'm saving PFObjects stored in an NSArray to file using NSKeyedArchiving. The way I do that is by implementing NSCoding via this library. For some reason unknown to me, several…
Apollo
  • 7,820
  • 27
  • 91
  • 177
1
2 3
36 37