我目前正在尝试轻松保存UIViewController的状态。但是,显然可能有同一类的控制器需要将它们的数据保存在同一目录中。
所以我正在寻找一种方法来获取实例的唯一标识符,以便将数据安全地写入磁盘。
提前致谢。
答案 0 :(得分:3)
答案是没有简单的方法可以做到这一点。因为那不是你应该做的。对象实例不是数据模型。在运行之间不保存对象之间的链接。但是,可以创建数据的标识符,并且对象可以跟踪这些标识符以及数据之间的相应链接。
e.g。您可以在数据模型中的对象之间创建链接,方法是在创建实例时分配UUID,然后在退出时将其保存到磁盘...在启动时从磁盘加载它。
我会研究NSCoding和核心数据......它们都旨在让您在退出应用时保留数据。
也许更新/提出一个新问题,澄清你想要实现的目标。
答案 1 :(得分:1)
我并非100%确定我已正确阅读了您的问题,但我确实已经阅读了两次。
这是你需要的吗?
@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