Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCoding with Nested Custom Objects?

I have a series of nested objects that I am needing to put through the NSCoding protocol so that I can save the top level object into NSUserDefaults.

Here is the structure of objects:

'Instructor' class

  • NSMutableArray that holds instances of...

'Class' class

  • NSMutableArray that holds instances of...

'Student' class

  • Name Property
  • Number Property
  • Money Property

I am needing to save an instance of Instructor to NSUserDefaults or to documents for the app. As you can see the Instructor object is holding an array that is then holding instances of a class. That class object is holding instances of students.

Is the NSCoding protocol recursive? What I mean by that is if I add the NSCoding protocol to each class, I could then save an Instructor object and it would recursively encode the contained objects?

Would it then work the same way while decoding? I could just decode the one instructor class and it would recursively decode the objects contained because they also conform to the NSCoding protocol?

How could I go about setting this up?

like image 839
BlueBear Avatar asked Sep 25 '13 19:09

BlueBear


People also ask

How does nscoding work?

As you can see, NSCoding is mostly boilerplate. Each property is encoded or decoded as an object or type, using the name of the property of as the key each time. (Some developers prefer to define NSString * constants for each keypath, but this is usually unnecessary).

How do I create a nested PowerShell custom object?

How do I create a nested PowerShell custom object to store layers (nested) of information? Use a hashtable with the PSCustomObject type accelerator, and specify PSCustomObject as the value to the Property Name (Key): [PSCustomObject]@ { PropertyName = [PSCustomObject]@ { NestedPropertyName = ‘NestedPropertyValue’ } }

What are non-nscoding-compatible classes?

Archiving non-NSCoding-compatible Classes: According to object-oriented design, objects should take responsibility for encoding and decoding to and from a serialization format. However, when a class doesn’t come with NSCoding support built in, it may be left up to class that uses it to help out.

What is the purpose of object encoding and decoding?

This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces). In keeping with object-oriented design principles, an object being encoded or decoded is responsible for encoding and decoding its instance variables.


2 Answers

Yes, you can (and probably should) write your support for NSCoding to be recursive. That's how NSCoding is supposed to work.

When your implement encodeWithCoder, simply call

[coder encodeObject: aProperty forKey: @"propertyName"];

on all your object's properties, including it's container properties.

Then make sure every object in your object's object graph also conforms to NSCoding.

For scalar properties, you can save the scalar value using NSCoder methods like encodeInt:forKey:

To save an object that conforms to NSCoding to user defaults, first convert it to NSData using the NSKeyedArchiver class method archivedDataWithRootObject, then save the resulting data into defaults. Quite simple, really.

like image 85
Duncan C Avatar answered Oct 13 '22 01:10

Duncan C


NSCoding isn't magic so it will work 'recursively' if your implementation of the encoding and decoding methods tells it to be.

Implement the NSCoding methods and pass the data to be encoded to the encoder. Implement the NSCoding methods in all of your custom classes so that when you encode the array all of the contents can be processed appropriately.

Be sure to call super if the classes superclass also implements NSCoding.

e.g.

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.arrayOfClasses forKey:@"arrayOfClasses"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self.arrayOfClasses = [decoder decodeObjectForKey:@"arrayOfClasses"];
}
like image 38
Wain Avatar answered Oct 12 '22 23:10

Wain