compare - comparing nil to integer in Objective-C -


in following case string nsstring

if (string.length < 1) {     return; } 

and string turns out nil if statement still evaluate correctly because in case nil evaluates 0.

however, recommended practice (by clang or apple) , there arguments against , doing closer to:

if (!string || string.length < 1) {     return; } 

it's common like:

if (string.length) {     // string not nil , string has non-zero length } else {     // either string nil or length 0 } 

there no need, in such case, check see if string nil or not.

when string nil, end doing [nil length]. calling method on nil pointer results in value of "zero". how "zero" interpreted depends on method's return type.

primitive types appear 0. bool appears no. pointers appear nil.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -