Reputation: 41
I have a array with 10 objects , sometimes out of 10 objects ,some objects tends to null
and if any of the object is null ,Its comes as empty in my UIPickerView
but I want If the value of any object is null then it should be skipped.. what I am trying to do is
if([ob1 isEqualToString:@""]){
NSArray *Ar =[NSArray arrayWithObjects:ob2,ob3,ob4,ob5,ob6,ob7,ob8,ob9,ob10,Nil];}
else if [ob2 isEqualToString:@""] {}
but this will be long chain if i skip any null value this way..what should be the better way.
Upvotes: 1
Views: 1602
Reputation: 3251
I had the same problem regarding nil objects and wrote a category on NS(Mutable)Array
for that.
So instead of:
NSMutableArray *array = [[NSMutableArray alloc] init];
if (ob0) [array addObject:ob0];
if (ob1) [array addObject:ob1];
if (ob2) [array addObject:ob2];
if (ob3) [array addObject:ob3];
You can use arrayWithObjectsNil
for skipping nil objects:
id obs[] = { ob0, ob1, ob2, ob3 }; // Assuming obj1 and obj3 are nil.
NSLog(@"%@", [NSArray arrayWithObjectsNil:obs size:sizeof(obs)]);
> ( 0, 2 )
Or use arrayWithObjectsNull
for transforming nil objects to [NSNull null]
:
NSLog(@"%@", [NSArray arrayWithObjectsNull:obs size:sizeof(obs)]);
> ( 0, "<null>", 2, "<null>" )
With the category it is also possible to use mutability and write it this way:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectNil:ob0]; // skip, if ob0 is nil
[array addObjectNull:ob1]; // add [NSNull null], if ob1 is nil
...
There are also other methods with the same logic.
You'll find the category named Nilus on GitHub.
Upvotes: 0
Reputation: 1299
You should use NSMutableArray
, instead of NSArray
. Like as below:
NSMutableArray *m_array = [NSMutableArray array];
//Now you can check your objects if nil or empty string @"" and add it to the array.
if(object1==nil || [object1 isEqualToString:@""])
{
//the string is empty do not add anything to array
}
else
{
//we have a proper object
[m_array addObject:object1];
}
//here you'll get the array will only proper objects.
//use this array to fill your pickerView data.
Upvotes: 0
Reputation: 2430
try to use this condition
if ((ob2 == [NSNull null]) && ob2 == nil) {
}
Upvotes: 0
Reputation: 889
First take an NSMutableArray
.
And an NSArray
which is initialize with all objects including null.
Then check if the object is not null then add on to the mutable array.
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0;i<10;i++) {
if (![[objectArray objectAtIndex:i] isEqualToString:@""]) {
[array addObject:[objectArray objectAtIndex:i]];
}
}
Where ObjectArray is NSArray contain all the objects.We can see it takes less amount of coding.
Upvotes: 0
Reputation: 318804
If any of the 10 objects can be nil
(note - use nil
for Objective-C object references) then you need to do it the hard way:
NSMutableArray *array = [[NSMutableArray alloc] init];
if (ob1) [array addObject:ob1];
if (ob2) [array addObject:ob2];
if (ob3) [array addObject:ob3];
// ...
if (ob10) [array addObject:ob10];
This checks if each object is nil
or not. If you also want to make sure it is not nil
and it has a non-zero length value, change each if
condition to:
if (ob1.length) ...
Upvotes: 1