ios - Objective C ternary operator vs if statements nil check -
im checking see if array nil , if want set counter variable 0 , length of array if array not nil.
nsuinteger count = 0; if (self.somearray == nil) { count = 0; } else { count = self.somearray.count; }
and works fine expected. when try this:
nsuinteger count = (self.somearray == nil) ? 0 : self.somearray.count
i [nsnull count] unrecognized selector
error. however, when this
([self.somearray iskindofclass:[nsnull class]]) ? 0 : self.somearray.count`
it seems latter should work me there simple overlooking? nsnull
objects nil right? why simple nil check working in if statement , not in ternary operator?
self.somearray
nsnull
object, not nsarray
object.
i suspect got object json message , haven't checked type before storing in class.
once have sorted; works if array set or not:
nsuinteger count = self.somearray.count;
Comments
Post a Comment