-4

I'm using NSMutableArray with object name myArray : When there is no data from server My mutable array print

In next line of code if check for count it crashes. How to resolve this please help me.

 NSLog(@"Print Array : %@", myArray);
 if(myArray.count > 0 ){

      Excute code
 }

Error:

Print Array : 2015-01-24 10:44:59.852 RemoteAccess[1061:60b] -[NSNull count]: unrecognized selector sent to instance 0x38b21a60

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

2

You think myArray is an array, but it's not - it's an instance of the class NSNull. Most likely, you're pulling it out of a JSON feed, and the JSON isn't returning an array, but rather null.

The simplest fix is to replace your conditional to check what class you actually got:

if ([myArray isKindOfClass:[NSArray class]] && myArray.count > 0) {
   // Do something
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
-1

Because your myArray is nil. You have to make sure that myArray is not nil.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29