0

I'd like to check for an empty object (i.e. an object of an array which doesn't have a value) within an array which gets its data from a file.

As an example, if my array contains 12 objects (all NSString) and the object at index 11 doesn't return a value when its description is printed into the debug section of Xcode. I want to check if that is the case and respond accordingly. I already tried

if (!([MY_ARRAY objectAtIndex:11] == nil))
{
   //Some Stuff
}
else
{
  //Some other Stuff
}

which didn't work.

Any help is appreciated.

8
  • What sort of objects are in the array? If one returns an empty string then it's for a reason specific to that type of object. E.g. if it's an NSString (which return themselves as their description) then it may just be the empty string. Commented Jun 30, 2014 at 19:57
  • Just strings, but wouldn't an empt string return @"" and not absolutely nothing? Commented Jun 30, 2014 at 20:04
  • You can check the length of the string: [string length] > 0 Commented Jun 30, 2014 at 20:06
  • Define "empty". Empty string, empty dictionary, empty array, empty set??? Commented Jun 30, 2014 at 20:44
  • @KJA1582 no, it wouldn't. The thing returned by description and therefore output by NSLog is a description of the object. It is not the Objective-C syntax you would need to type to create the object. Can you imagine what something like NSURLConnection would have to supply as a description? Commented Jun 30, 2014 at 21:45

4 Answers 4

2

The description method is for debugging. You should not use it in your program logic. What are these objects, and what do they contain? Can you modify the objects to add an "isEmpty" property?

If you use NSNull, you'd use code like this:

NSArray *array = @{@"String", @(4), [NSNull null], @"Another string");

for (id anObject in array)
{
  if (anObject =! [NSNull null]))
  {
     //Some Stuff
  }
  else
  {
    //Some other Stuff
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can check the length of the string: [string length] > 0

Comments

0

an object is an array cannot be nil, but you can use [NSNull null] which is an "object equivalent" to nil

11 Comments

It still executed the if and not the else after replacing nil with [NSNull null]
I get following in the Debug console after printing the description of said object: Printing description of [11]: <object returned empty description>
If an object returns empty description, it just means it does not implement the description method. What is the class of the object if you po the object in console, or NSLog the object in code?
Using NSLog it just prints nothing, but what is po?
Nvm, figured it out. This is the result: (lldb) po 0x3faa29d0 <object returned empty description> (lldb)
|
0

As Jerome Diaz states, objects in an array can't be nil. The only option you have is to check the count property of the array if it reflects an expected value, or you can inspect the type/class of the object in the array. A safe way to include empty object into array is [NSNull null], but this is the task for the method that fills the array, not the one that reads it.

You can check the class type of an object in array with isKindOfClass or isMemberOfClass.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.