ary*_*axt 47 cocoa-touch objective-c nscoder
我的类有以下方法,它打算加载一个nib文件并实例化该对象:
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self = [super initWithCoder:aDecoder]) {
// Do something
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
如何实例化这个类的对象?这是什么NSCoder
?我该如何创建它?
MyClass *class = [[MyClass alloc] initWithCoder:aCoder];
Run Code Online (Sandbox Code Playgroud)
小智 41
您还需要定义以下方法,如下所示:
- (void)encodeWithCoder:(NSCoder *)enCoder {
[super encodeWithCoder:enCoder];
[enCoder encodeObject:instanceVariable forKey:INSTANCEVARIABLE_KEY];
// Similarly for the other instance variables.
....
}
Run Code Online (Sandbox Code Playgroud)
并在initWithCoder方法中初始化如下:
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
self.instanceVariable = [aDecoder decodeObjectForKey:INSTANCEVARIABLE_KEY];
// similarly for other instance variables
....
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
您可以初始化对象标准方式,即
CustomObject *customObject = [[CustomObject alloc] init];
Run Code Online (Sandbox Code Playgroud)
Jac*_*ack 17
所述NSCoder
类用于对象的归档/解除存档(编组/解组,序列化/反序列化).
这是一种在流(如文件,套接字)上编写对象并能够在以后或在不同位置检索它们的方法.
我建议你阅读http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Archiving/Archiving.html
归档时间: |
|
查看次数: |
64052 次 |
最近记录: |