1

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.

1
  • you should first try filtering your array and remove the null values then use it for UIPickerView. Commented Sep 6, 2013 at 4:46

5 Answers 5

1

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) ...
Sign up to request clarification or add additional context in comments.

4 Comments

for me if value of object isequaltostring @"" then it should be skipped
OK, then change all of the if statements to check the length. Please note that your question states that some of the objects are nil. That is VERY different from having a value of @"". If none are actually nil, then you would be better off to have the objects in an array instead of 10 separate variables. Then you can create a new array with just the non-empty (not non-nil) values using a simple loop.
@rmaddy you are checking for each object is write.Don't you think so it will increase your file size unnecessary.Instead you can fill in an NSArray with one line then filtered out. Many lines of code may be omit.
@iPhoneDev In the question, the OP states that some of the objects might be nil. You can't put nil objects in an array. Plus, the original code has 10 separate variables. My answer was based on having 10 variables where some could be nil. If it turns out that none are actually nil, then using 10 variables is not a good approach.
0

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.

3 Comments

How will this work? The problem is that some of the objects are nil pointers. Therefore they can't be in objectArray to begin with.
If you use initWithObjects:, the array won't have any values past the 1st nil object. Try it. Use initWithObjects: and pass in 10 variables. Make sure some of the variables are nil. See what ends up in the array.
sorry my mistake as he is checking with empty string I assume that all are strings.you can see isEqualToString is used on main question.
0

try to use this condition

if ((ob2 == [NSNull null]) && ob2 == nil) {

}

3 Comments

ob2 = [NSNull null] assigns [NSNull null] to ob2.
@NateChandler that was typing mistake and it doesn't assign anything because that is under condition.
@eptdeveloper Sorry but using = in an if statement will do the assignment and the value of the expression will be the value of the assignment.
0

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.

7 Comments

Your if statement is incorrect. If object1 is nil, your code then tries to add object1 to the array. That will crash.
That's better. But of course there is no need to check for nil in Objective-C. You can safely do if (object.length == 0). This will be true if object1 is nil or if it is the empty string.
object.length==0 returning true, is sort of a hack-type programming. It relies on the mercy of compiler that an object that does not exists returns a value 0. That's improper coding practice. But, yeah its one another way to achieve it.
I don't think it's a hack. It's a reliance on the Objective-C specification that nil objects can be sent messages and the result is always 0.
@CodenameLambda1 rmaddy is correct. See Apple's Programming with Objective-C guide. As described there: "If you expect a return value from a message sent to nil, the return value will be nil for object return types, 0 for numeric types, and NO for BOOL types. Returned structures have all members initialized to zero."
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.