Reputation: 2649
I am having one array where i am fetching data from database for some specific value,and hence the key where that particular value is not there,it is fetching null value in the array.
Now,my question is how can i eliminate that null value from the array? I tried to fetch the nsarray value to nsstring and then to another array,but then it is not fetching the whole array,but takes the last indexed value.
Please help me out. Thank you.
I am attaching code for further reference :
NSString *url = [NSString stringWithFormat:@"webservice from where i m fetching data"];
NSLog(url);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod: @"POST"];
NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
dataArray=[[returnString JSONValue] retain];
[self setAlldetails:dataArray];
nsarray *InteName = [alldetails valueForKey:@"IntelligenceName"];
So the array fetches value for array InteName like this :
(
"00:00:00",
"00:00:00",
"17:15:00",
"17:15:00",
"17:15:00",
"17:15:00",
"00:00:00",
"00:00:00",
"17:15:00",
"17:15:00",
"17:15:00",
"17:15:00",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>"
)
so now,i want to remove occurance of the value null.
any guesses??
Upvotes: 2
Views: 4462
Reputation: 2649
Got the solution while doing T & E :
NSMutableArray *array = [[NSMutableArray alloc]init];
array = [InteName retain];
NSLog(@"Array : : %@",array);
NSMutableArray *array1 = [[NSMutableArray alloc]init];
NSString *str;
for(int i=0;i<[array count];i++)
{
str = [array objectAtIndex:i];
if(str !=[NSNull null])
{
[array1 addObject:str];
//[array removeObjectAtIndex:i];
}
}
Upvotes: 6
Reputation: 104065
NSMutableArray *nulls = [NSMutableArray array];
for (id candidate in dataArray)
if (candidate == [NSNull null])
[nulls addObject:candidate];
[dataArray removeObjectsInArray:nulls]; // assuming dataArray is mutable
Upvotes: 0
Reputation: 18670
Not sure if I am reading your question correctly, but assuming you want to get rid of the null entries in your array and that they are actually @"<null>"
strings:
NSMutableArray *array = ...;
for (id nullObject in array)
{
if ([nullObject isKindOfClass:[NSString class] && [nullObject isEqualToString:@"<null>"]
{
[array removeObjectAtIndex:i];
}
}
Upvotes: 1