Ramuel
Ramuel

Reputation: 59

exclamation mark before a NSString

Please do tell me what does an exclamation mark mean before a NSString

NSString *theString;
if (!theString);

cheers;

Upvotes: 1

Views: 1016

Answers (2)

Thomson Comer
Thomson Comer

Reputation: 3919

! is the boolean negation operation. It inverts YES to NO and vice-versa. NO is always equal to 0, while YES is any non-zero value, and as Jeremiah points out, a nil pointer is one that is set to 0x0, or decimal 0. Any pointer that isn't nil has a boolean value of TRUE. So, (!theString) is equivalent to (theString == nil).

Upvotes: 3

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30989

The ! symbol here (and in front of any expression whose type is a pointer) returns a true (1) result if the pointer is NULL and false (0) otherwise. The !theString expression is then just a short way of saying theString == NULL.

Upvotes: 2

Related Questions