Damian Van de Kauter
Damian Van de Kauter

Reputation: 67

Remove object from NSArray by index

I have a code that removes an object from an NSCollectionView, but it only removes one item. In the NSArray ("array") the value is "2 4" which returns 2, 4. But when I run the code, it only removes "2" and not "4".

The Log:

Click here for the image of the NSLOG.

The Code

NSString* LibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPathSMSettings = [LibraryPath stringByAppendingString:stormarManagerSettingsPlistPath];
NSString *betasDef = [SMInAppCommunicationDEF stringByAppendingString:@"BETA App Id"];

NSString *indexOfApp = [Functions readDataFromPlist:plistPathSMSettings ForKey:betasDef];

if (!(indexOfApp == nil)) {
    NSArray * array = [indexOfApp componentsSeparatedByString:@" "];
    NSLog(@"Array: ", array);
    int currentValue = 0;
    for (int i = 0; i < [array count]; i++)
    {
        currentValue = [(NSNumber *)[array objectAtIndex:i] intValue];
        NSLog(@"currentValue: %d", currentValue); // EXE_BAD_ACCESS
            NSLog(@"x: %d", i);
            [self.content removeObjectAtIndex:(currentValue)];
            [self.collectionView setContent:self.content];
            [self.collectionView reloadData];
            NSLog(@"next: %d", currentValue); // EXE_BAD_ACCESS

    }
}
else if (indexOfApp == nil) {
    [self.collectionView setContent:self.content];
    [self.collectionView reloadData];
}

Upvotes: 1

Views: 949

Answers (2)

Hazneliel
Hazneliel

Reputation: 541

This is what is happening, lets say content is this:

 0  1  2  3  4
[A][B][C][D][E]

And your array is 2,4 as you said. Then on the first iteration, 2 should be removed:

 0  1  2  3  4
[A][B][ ][D][E]

The content is now:

 0  1  2  3
[A][B][D][E]

Since an element has been removed, the length of content has changed, if you try now to remove the element at 4, nothing will happen since there is no element at that position.

Try sorting your array before start removing them from content:

NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];

Then remove elements from sortedArray, not from array

Upvotes: -1

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

Suppose you have a mutable array that contains [a, b, c, d], index of a is 0, b 1, c 2, d 3. But if you remove say element at index 1, then the array contains [a, c, d] and elements have now different indexes a is 0, c is 1 and d is 2...

Your array is an array of indices, so that you tried to remove element at index 2 (the third), and then after that removal element at index 4 (the fourth) but initially at index 5 (as 4>2)... Is it really what you want? [e0, e1, e2, e3, e4, e5, e6...] --> remove at index 2 --> [e0, e1, e3, e4, e5, e6...] ---> remove at index 4 --> [e0, e1, e2, e3, e4, e6...]?

--Add a solution--

A good fix is to sort in descending order indices and them remove the elements, ie if indices are [5,2,7,1] --> sort [7, 5, 2, 1] --> remove 8th, then 6th, then 3rd then 2nd. This way ensure that removing an element at a given index will not change the indexing of preceding elements.

Upvotes: 2

Related Questions