Rajesh G
Rajesh G

Reputation: 17

Removing negative numbers from NSArray in Objective C

I am new to Objective C. In my application, I am having array of data, in which I need only positive numbers and need to delete negative numbers.

result = [NSMutableArray arrayWithObjects: @"1",@"2",@"3","-4","-5","6","9",nil];
NSLog(@"  Before Remove  %d", [result count]);
NSString *nullStr = @"-";
[result removeObject:nullStr];

How to achieve this? Any pointers?

Upvotes: 1

Views: 1404

Answers (5)

Kirsteins
Kirsteins

Reputation: 27345

NSIndexSet *indexesOfNegativeNumbers = [result indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return [(NSString *)obj hasPrefix:@"-"];
}];

[result removeObjectsAtIndexes:indexesOfNegativeNumbers];

Upvotes: 0

Gabriele Petronella
Gabriele Petronella

Reputation: 108169

You can use a predicate to filter the array

NSArray * numbers = @[@"1", @"2", @"3", @"-4", @"-5", @"6", @"9"];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"integerValue >= 0"];
NSArray * positiveNumbers = [numbers filteredArrayUsingPredicate:predicate];

Result

[@"1", @"2", @"6", @"9"]

Also note that this will work with both an array of NSNumbers and an array of NSStrings, since they both feature the integerValue method.

Upvotes: 6

Abizern
Abizern

Reputation: 150755

If you have an array and you want to filter items out of it, then using an NSPredicate is a good way to do it.

You haven't said whether your array contains NSNumbers or NSStrings, so here's a demonstration of how to use predicates to filter an array in both cases

// Setup test arrays
NSArray *stringArray = @[@"-1", @"0", @"1", @"2", @"-3", @"15"];
NSArray *numberArray = @[@(-1), @0, @1, @2, @(-3), @15];

// Create the predicates 
NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:@"integerValue >= 0"];
NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF >= 0"];

// Filtering the original array returns a new filtered array
NSArray *filteredStringArray = [stringArray filteredArrayUsingPredicate:stringPredicate];
NSArray *filteredNumberArray = [numberArray filteredArrayUsingPredicate:numberPredicate];

// Just to see the results, lets log the filtered arrays.
NSLog(@"New strings %@", filteredStringArray); // 0, 1, 2, 15
NSLog(@"New strings %@", filteredNumberArray); // 0, 1, 2, 15

This should get you started.

Upvotes: 0

Ultrakorne
Ultrakorne

Reputation: 1303

the one you wrote is an array of strings, if that's ok you can loop the array and remove strings that starts with - using

Since you cannot remove objects while iterating, you can create a new array and store only positive numbers (or mark item to be deleted and delete after the loop)

NSMutableArray onlyPositives = [[NSMutableArray alloc] init]
for(int i=0; i < [result count]; i++)
{
    if(![result[i] hasPrefix:@"-"])
        [onlyPositives add:[result[i]]
}

Upvotes: 2

CRDave
CRDave

Reputation: 9285

Try This:

for(NSString *number in [result copy])
{
    if([number intValue] < 0)
    {
         [result removeObject:number];
    }
}

Upvotes: 0

Related Questions