Deepak Thakur Deepak Thakur - 1 year ago 78
Objective-C Question

Remove null objects from mutable array in objective c

I have a mutable array of checked box

NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithObjects:namePropertyString, lastNamePropertyString, companyPropertyString, workEmailPropertyString, personalEmailPropertyString, workPhonePropertyString, cellNumberPropertyString, nil];
[arrayOfCheckedBox removeObjectIdenticalTo:[NSNull null]]; //not working
NSLog(@"array of check box = %@", arrayOfCheckedBox);

If I click on check boxes at index 0, 1 and 4, it will only collect object at indexes 0 and 1 only and will not detect index 4 at all.

I get the values at the selected index in log before getting it in arrayOfCheckedBox. How to get checked values in this case?


The problem is that you're hitting a nil value, so the arrayWithObjects: method thinks you're at the end of the list of objects.

Something like this will work:

NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithCapacity:7];

if (namePropertyString)
    [arrayOfCheckedBox addObject:namePropertyString];

if (lastNamePropertyString)
    [arrayOfCheckedBox addObject:lastNamePropertyString];