Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an NSArray object when encoding?

I'm building an application that utilises NSCoding to save NSObject's to a documentPath. I'm having no issues doing this, I'm just curious about something.

I have MACompany, which implements NSCoding delegate methods.

- (void) encodeWithCoder:(NSCoder *)encoder {

     [encoder encodeObject:address_1 forKey:kAddress_1];
     [encoder encodeObject:address_2 forKey:kAddress_2];
     [encoder encodeObject:city_town forKey:kCity_Town];
     [encoder encodeObject:company_name forKey:kCompany_Name];
     [encoder encodeObject:country forKey:kCountry];
     [encoder encodeObject:date_added forKey:kDate_Added];
     [encoder encodeObject:fax forKey:kFax];
     [encoder encodeObject:parent_company_website forKey:kWebsite];
     [encoder encodeObject:postal_code forKey:kPostal_Code];
     [encoder encodeObject:state_province forKey:kState_Province];
     [encoder encodeObject:type forKey:kType];

     [encoder encodeObject:stores forKey:kStores]; //NSArray of custom NSObjects

}

As you can see, I have a NSArray of custom NSObjects (MAStore). Each one of these objects also implements the same NSCoding what not.

However, my question is, when I call encodeWithCoder:(NSCoder *)encoder method in MAStore, and it gets to the [encoder encodeObject:stores forKey:kStores], will all the objects stored within the stores NSArray have the encoderWithCoder:(NSCoder *)encoder method called if implemented?

EDIT

The reason I'm asking this is that I want to know whether or not it'll work before I invest time in doing such a thing. I have multiple custom NSObjects with NSArrays that hold more custom NSObjects. It would be a long process to find that it doesn't work.

like image 390
Sebastien Peek Avatar asked Nov 03 '11 01:11

Sebastien Peek


1 Answers

Yes. The encoder recursively walks into container objects (arrays, dictionaries, sets, etc) and encodes all of those objects as well. You'll hit an exception if one of the inner objects doesn't support NSCoding.

like image 154
Ben Zotto Avatar answered Sep 19 '22 20:09

Ben Zotto