andreamazz / AMTagListView

UIScrollView subclass that allows to add a list of highly customizable tags.
MIT License
758 stars 110 forks source link

[__NSArrayM insertObject:atIndex:]: object cannot be nil #37

Closed gautierdelorme closed 8 years ago

gautierdelorme commented 9 years ago

Hi ! I have an issue in rearrangeTags on this line : [self addSubview:obj];

Sometimes I got this exception : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

But when I do this to debug :

if (obj != nil) {
            NSLog(@"add %@", obj.tagText);
            [self addSubview:obj];
        }

the NSLog print the good tagText and I got the same error so I don't understand... Thanks for your help !

andreamazz commented 9 years ago

Hi @gautierdelorme Can you reproduce the issue in the sample project by any chance?

gautierdelorme commented 9 years ago

No I can't but let me explain you my situation : I use AMTagListView in a custom UITableViewCell and in other view in an other viewController and I feel the bug happen when I show the custom UITableViewCell and I go to the other viewController right after.

Here how I add tags in my custom UITableViewCell :

-(void) setPost:(SCCurablePost *)post {
    _post = post;
    if (post.tags) {
        [self.tagListView removeAllTags];
        [self.tagListView addTags:post.tags];
        [self updateLength];
    }
}

and in my other ViewController :

[self.dataSource loadKeywordsWithSuccess:^{
        if (self.dataSource.keywords) {
            [_keysView.keywordListView addTags:self.dataSource.keywords];
            [_keysView.keywords addObjectsFromArray:self.dataSource.keywords];
        }
        [_keysView updateLength];
        [self updateLengthKeywords];
    }failure:^(NSError *error) {
    }];

Do you have any idea of the problem ?

andreamazz commented 9 years ago

Hard to say without being able to debug the code. Try to call addTags:tags andRearrange:NO, just to make sure that the issue is in the rearrange.

gautierdelorme commented 9 years ago

When I call addTags:tags andRearrange:NO I got the error because of the call in : (void)layoutSubviews and when I call addTags:tags andRearrange:YES and comment the line like this :

- (void)layoutSubviews {
    [super layoutSubviews];
    //[self rearrangeTags];
}

I got the error because of the call in removeAllTags so I created :

- (void)removeAllTagsAndRearrange:(BOOL)rearrange{
    for (AMTagView *tag in self.tags) {
        [tag removeFromSuperview];
    }
    [self.tags removeAllObjects];
    if (rearrange)
        [self rearrangeTags];
}

But even if I use this function and then I call addTags:tags andRearrange:YES I got the same error...

andreamazz commented 9 years ago

Can you reproduce your config in a separate project that I can debug?

andreamazz commented 8 years ago

Guess not. Closing this, feel free to reopen if you need to.