0

how to remove empty or Null value from Nsarray in objective c

here my sample code :

cell.subtitleLbl.text = [[chapterArray objectAtIndex:indexPath.row] valueForKey:@"person"];
Anand
  • 71
  • 1
  • 11

2 Answers2

1

Choice-1

// mainArray array is your current array
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [mainArray count]; i++) {
    id obj = [mainArray objectAtIndex:i];
    if (![obj  isKindOfClass:[NSNull class]]) { // or if (![obj  isKindOfClass:[[NSNull null]class]]) {
        [tempArray addObject:obj];
    }
}

NSLog(@"%@",tempArray);

Choice-2

[yourarrayName removeObjectIdenticalTo:[NSNull null]];

Choice-3

ussing Predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF CONTAINS %@)", @"<null>"];
[yourarrayname filterUsingPredicate:predicate];

Choice-4

if you using array of dictionary

NSMutableArray *newarray=[NSMutableArray array];
for (NSDictionary *dictionary in presentArrray)
{
    if (dictionary[key] != [NSNull null]) {
        [newarray addObject:dictionary];
    }
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

use can Use Mutable Array

Remove null Objects from it

[arrayName removeObjectIdenticalTo:[NSNull null]];
viratpuar
  • 524
  • 1
  • 5
  • 22