Reputation: 3389
I'm currently trying to save a UIViewController's state easily. However it's obviously possible that there are to controllers of the same class that need to save their data in the same directory.
So I'm looking for a way to get unique identifiers for an instance to write the data safely to the disk.
Thanks in advance.
Upvotes: 2
Views: 1695
Reputation: 12107
The answer to this is there is no simple way to do this. Because that's not what you're supposed to be doing. Object instances are not a data model. Linkages between objects are not saved between runs. However, identifiers for data can be created, and objects can keep track of those identifiers, and corresponding linkages between data.
e.g. You can create linkages between objects in your data model, by assigning a UUIDs when you create an instance, and then saving that to disk when you quit... loading it from disk when you launch.
I'd look into NSCoding and Core Data... they're both designed to allow you to persist your data when you quit your app.
Perhaps update/ask a new question, clarifying what you're trying to achieve with this.
Upvotes: 3
Reputation: 32066
I'm not 100% sure I've read your question correctly, but I did read it twice.
Is this what you need?
@interface MyClass : NSObject
{
int myID;
}
@end
//this is where the magic is. Keeps a static identifier which starts at zero each time you start the app and increments each time you init this class.
static int identifier = 0;
@implementation MyClass
-(id) init
...
identifier ++;
myID = identifier;
...
}
....
@end
Upvotes: 1