我在取消归档具有循环引用的 NSMutableSet 时遇到问题
我在 iPhone (iOS 4.3) 应用程序中解档 NSMutableSet 对象时遇到问题。
我有一个 UIImageView 的子视图,其中包含 NSMutableSet 类型的 ivar。我在标头中定义了 NSMutableSet(并在实现中综合了它):
@interface MyView : UIImageView
{
NSMutableSet *group;
}
@property (nonatomic, retain) NSMutableSet *group;
该集合包含对零个或多个 MyView 类型对象的引用。由于组是 MyView 的一个属性,因此对非零组成员(MyView 对象)的引用是循环的。
我像这样编码这个视图:
- (void) encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"Encoding group for %@ -- %@", self.imageFilename, self.group);
[aCoder encodeObject:self.group forKey:@"group"];
}
NSLog 显示正在归档的健康组对象。但是当我取消编码时,我收到一个 NSException:
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithFrame:CGRectZero];
self.group = [aDecoder decodeObjectForKey:@"group"]; // <- NSException here.
return self;
}
第一次看到这个错误时,我收到了一条相当详细的错误消息: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSCFSet count]:发送到未初始化的可变集对象的方法”
但是,当我重现问题时,我得到的信息要少得多信息: 抛出“NSException”实例后调用终止
该存档是使用以下命令创建的:
[NSKeyedArchiver archiveRootObject:parentOfMyView toFile:myFilepath];
并使用此命令打开:
[NSKeyedUnarchiver unarchiveObjectWithFile:myFilepath];
感谢您对此的任何帮助。
- 富有的
I'm having problems unarchiving an NSMutableSet object in my iPhone (iOS 4.3) app.
I have a subview of UIImageView that contains an ivar of type NSMutableSet. I have defined the NSMutableSet in the header (and synthesized it in the implementation):
@interface MyView : UIImageView
{
NSMutableSet *group;
}
@property (nonatomic, retain) NSMutableSet *group;
The set contains references to zero or more objects of type MyView. Since group is an attribute of MyView, the references to non-nil group members (MyView objects) are circular.
I encode this view like so:
- (void) encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"Encoding group for %@ -- %@", self.imageFilename, self.group);
[aCoder encodeObject:self.group forKey:@"group"];
}
The NSLog shows a healthy group object being archived. But when I unencode, I get an NSException:
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithFrame:CGRectZero];
self.group = [aDecoder decodeObjectForKey:@"group"]; // <- NSException here.
return self;
}
The first time I saw this bug, I got a fairly detailed error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet count]: method sent to an uninitialized mutable set object'
However, when I reproduce the problem, I get a far less informative message:
terminate called after throwing an instance of 'NSException'
The achive is created with this command:
[NSKeyedArchiver archiveRootObject:parentOfMyView toFile:myFilepath];
And opened with this one:
[NSKeyedUnarchiver unarchiveObjectWithFile:myFilepath];
Thanks for any help with this.
-- rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论