The text in the text field is the value of the NSString instance or nil ; it is never equal to an instance of the NSNull class (which does not match nil ). Since the first comparison is always true, then the entire if-condition evaluates to true and a message appears.
You can fix the if condition for
if (txtName.text != nil && txtName.text.length != 0 )
or, since sending a message of length in nil will return 0, itβs still easy
if (txtName.text.length != 0 )
although I usually use the 1st option with 2 comparisons
source share