开发者

I'm having problems un-archiving an NSMutableSet with circular references

开发者 https://www.devze.com 2023-02-19 20:05 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号