2

I have an NSArray which contain string values, but sometimes it may contain null values also. I am getting that array like this,

(
    "<null>",
    "<null>",
    "<null>",
    "<null>",
    "<null>"
)

I want to replace above by,

(
    " ",
    " ",
    " ",
    " ",
    " "
)

That means I want to replace <null> by an empty string. how to achieve that ? I am aware of some string methods but that can not be implemented on array.

iOSNoob
  • 1,420
  • 2
  • 16
  • 30

6 Answers6

4
NSMutableArray *arr = [myNullArray mutableCopy];

for (int i=0; i < [arr count]; i++)
{
  if ([[arr objectAtIndex:i] isKindOfClass:[NSNull Class]])
  {
     [arr inserObject:@" " atIndex:i];
  }
}

NSLog(arr);
BhushanVU
  • 3,455
  • 1
  • 24
  • 33
1

you would have to use the answer from here and iterate through each string in the array and perform the replacement of "<null>" with " "

edit: i think i misunderstood your question, if the array is containing NSNull objects, then you would still need to iterate through the array and just do a check like

for(int i = 0; i < array.count; i++){
 if([array[i] isKindOfClass:[NSNull class]]){
  array[i] = @" ";
 }
}

disclaimer: havent tested code, just doing it off top of my head

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
1

First we need to understand, if it is a null string or the description taken from the log of an NSNull object. I suppose that array is your array. In the first case:

 NSMutableArray * mutArr = @[].mutableCopy;
    for (id obj in array) {
        if ([obj isKindOfClass:NSString.class]) {
            if ([obj isEqualToString:@"<null>"]) {
                [mutArr addObject:@""];
            }
            else {
                [mutArr addObject:obj];
            }
        else {
            [mutArr addObject:obj];

        }
    }
    array = mutArr.copy;

But I guess is the second:

NSMutableArray * mutArr = @[].mutableCopy;
    for (id obj in array) {
        if (obj == [NSNull null]) {
                [mutArr addObject:@""];
            }           
        else {
            [mutArr addObject:obj];

        }
    }
    array = mutArr.copy;

Just few hints that maybe can interest you:

  • If the original array is an NSArray is immutable and you can't change its content
  • during iteration don't remove/add object from the same array you are using for iteration or it will raise an exception
  • Mosto of times is the description of an NSNull object. NSNull is sneaky, because it can handle just few method and it's always source of crashes due to doesn't recognizeSelector
  • Fast enumeration is faster than common C for loop
  • swapping and substituting object in an array requires times
Andrea
  • 26,120
  • 10
  • 85
  • 131
1

You can try something like this.

for (NSInteger i = 0; i < array.count; i++) {
    if ([array[i] isEqual:[NSNull null]] || [array[i] isEqual:@"<null>"]) {
        [array replaceObjectAtIndex:i withObject:@" "];
    }
}
gabbler
  • 13,626
  • 4
  • 32
  • 44
0

If you are using swift this would one way.

var stringArray = [String?]()

// Fill array with you stuff
// ...

// Check null (nil)
for string in stringArray
{
    if string == nil
    {
        string = " "
    }
}
OwlOCR
  • 1,127
  • 11
  • 22
0
   NSMutableArray *anArray = [NSMutableArray arrayWithArray:yourArray];  
     for (id anObject in anArray) {

     if((anObject) && (anObject != nil) && !([anObject
     isKindOfClass:[NSNull class]]))   
     { 
          // if object is not nil  
     }
 else{
           anObject = @"";  
      } 
 }

    yourArray = (NSArray *)anArray;
Pratap Vhatkar
  • 691
  • 10
  • 21