Quote:
Originally Posted by AndrewSpeaksOut
Here's what I did to remove null items from an NSMutableArray:
Code:
for (int i=0; i<[components count]; i++) {
if ([components objectAtIndex:i] == [NSString stringWithFormat:@""]) {
NSLog(@"nil found");
} else {
[muta addObject:[components objectAtIndex:i]];
}
}
Thanks for the help guys!
|
Hi,
Remember that you can not use == to see if a string is empty. You also can not insert nil/null into an array as it will raise an exception and the program will crash.
So what you can do is something like this:
Code:
NSMutableArray *MyArray = [[NSMutableArray alloc] init];
[MyArray addObject:@"One"];
[MyArray addObject:@""];
[MyArray addObject:@"Three"];
int Counter = 0;
for (Counter = 0; Counter < [MyArray count]; Counter++){
NSString *ThisString = (NSString*) [MyArray objectAtIndex:Counter];
if ([ThisString length] == 0){
[MyArray removeObjectAtIndex:Counter];
Counter = -1;
continue;
}
}
[MyArray release];
MyArray = nil;
I haven't written the above code in XCode. I just wrote it here so it might have some syntax errors but I think you get the idea. Make sure you use [NSString length] instead of what you did. Best of luck.