1

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

richardsun
  • 3,245
  • 1
  • 18
  • 22
  • Hey, i delited my answer because it wasn't right. I checked your question again and I seams ok. Try renaming group to something else. I'm sure the bug is not in posted code but elsewhere. – xpepermint Apr 13 '11 at 19:17
  • Thanks xpepermint! I implemented a workaround where I instantiate each archived group's MyView instances lazily using a unique key that I assign when the MyView object is first created. – richardsun Apr 20 '11 at 15:13

0 Answers0