2

I am trying to save a lot of objects to a file and retrieve them for later use.

Previously, I used these macros - it felt tedious though.

Then I discovered Autocoding, which is awesome in its own way but it can't encode/decode instance variables.

I am in need of automating the whole process of encoding/decoding since my app is still under development and it's tedious to write encode/decode implementation for every ivar.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
riyaz
  • 1,093
  • 1
  • 8
  • 21
  • 1
    Can you use properties instead of ivars? – jervine10 Feb 19 '15 at 17:25
  • Ya I can, but I cant force all my team mates to do so. Hope you understand my situation. – riyaz Feb 19 '15 at 17:27
  • Do you have a solution? @AaronBrager – riyaz Feb 19 '15 at 17:29
  • 1
    Doing things like this makes upgrading difficult. If your class implementation changes, you may have to write and maintain painful serialization upgrade methods. – KirkSpaziani Feb 19 '15 at 17:31
  • 1
    That being said, I answered a similar question a little while ago: http://stackoverflow.com/questions/28054122/using-nscoder-and-nskeyedarchiver-with-runtime-reflection-to-deep-copy-a-custom/28054556#28054556 – KirkSpaziani Feb 19 '15 at 17:32
  • 1
    @riyaz You could try convincing them, or yelling at them, or reverting their commits that use ivars. – Aaron Brager Feb 19 '15 at 17:54

2 Answers2

5

You could just overide the +codableProperties method and return the names of the ivars. It's not as nice as detecting them automatically, but nicer than writing the NSCoding logic manually.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • 1
    This might be the best solution; there are some real pitfalls with a completely automatic approach. – Caleb Feb 19 '15 at 18:55
1

You can get the list of ivars associated with a class from the Objective-C runtime -- see the class_copyIvarList function. Likewise, you can get the value of an ivar for a specific object using object_getIvar and set it with object_setIvar. These seem like the main tools you'd need to automatically encode an object's ivars.

One place you'll need to be careful is that you won't want to save both the value for a property and the value for the ivar that backs it. So you'll probably want to compare the list of ivars to the list of properties, and only encode/decode those ivars for which there doesn't seem to be a corresponding property.

Also, it's not always desirable to make every last bit of an object's state persistent, so you should consider providing a way to opt out for specific ivars (which might in the end be just as tedious as opting in). As well, you'll probably need to determine which ivars you even can encode, e.g. the usual value types and any types which themselves implement NSCoding.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    [Here's a good sketch of an implementation](http://funwithobjc.tumblr.com/post/1488943727/aspect-oriented-programming) (but it doesn't resolve all of Caleb's concerns). – Aaron Brager Feb 19 '15 at 17:56
  • extending `codableProperties` and and making use of `class_copyIvarList`, `object_getIvar`, `object_setIvar` etc solves my problems. //it's not always desirable to make every last bit of an object's state persistent, so you should consider providing a way to opt out for specific ivars// I should have to find a way to solve this problem. thanks – riyaz Feb 20 '15 at 07:11