Crystal
Crystal

Reputation: 29518

removeAllObjects for NSMutableArray that populates UITableView crashes with ARC

I have a property of

@property (nonatomic, strong) NSMutableArray *timesArray;

It is used to populate the data in my UITableView. When I want to clear my view, I do this:

- (void)clearView {
    self.nameField.text = @"";
    self.noteField.text = @"";
    if ([_timesArray count] > 0) {
        [self.timesArray removeAllObjects];
        [self.customTableView reloadData];
    }
}

The removeAllObjects causes a crash. I am not sure why. I looked around and a lot of posts talk about an object being overreleased. How is that happening if I'm using ARC and not calling retain/release on any objects. My _timesArray just holds NSDate objects I get from a UIDatePicker.

My stack trace looks like:

enter image description here

My insertPill looks like:

- (void)insertPill:(id)sender {
    //[[NSNotificationCenter defaultCenter] postNotificationName:InsertPillNotification object:self];

    [self clearView];
}

If I don't removeAllObjects, and just do:

NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithCapacity:0];
self.timesArray = emptyArray;

This works. But I'd still like to know why by removing the objects it does not work.

Edit: I initialize the array in viewDidLoad:

_timesArray = [[NSMutableArray alloc] initWithCapacity:0];

When I want to add a new object to the array, I do this:

NSMutableArray *tempUnsortedArray = [[NSMutableArray alloc] initWithArray:_timesArray];        
        [tempUnsortedArray addObject:_datePicker.date];
        self.timesArray = tempUnsortedArray;

I'm not sure if the way I'm adding data the array is causing the issue or not.

Upvotes: 0

Views: 800

Answers (2)

ThomasW
ThomasW

Reputation: 17317

You're getting a doesNotRecognizeSelector: exception. This probably means that the object you think is a NSMutableArray is not really one. It is probably an NSArray. Where are you assigning the object?

To start debugging the issue, po the object before calling removeAllObjects. What type of object is it reported as?

Otherwise it could be possible that there is a non NSObject element in timesArray.

Upvotes: 1

SentineL
SentineL

Reputation: 4732

@property (nonatomic, strong) NSMutableArray *timesArray;
if ([_timesArray count] > 0) 

It seems, you syntesize your property like this:

@syntesize timesArray = _timesArray;

You chacking count of _timesArray, but removing object from timesArray. I never set new name for my properties and don't sure how it works, but I dont like it.

Upvotes: 0

Related Questions