hottea
hottea

Reputation: 37

assignment and comparison operator on same line

I'm trying to figure out what this is doing or what it would be shorthand for?

_var1 = _var2 == nil;

I have been testing with this

_thing1 = 1;
_thing2 = 2;
_thing3 = 3;

NSLog(@"thing1 before %li",(long)_thing1);
NSLog(@"thing2 before %li",(long)_thing2);
NSLog(@"thing2 before %li",(long)_thing3);

_thing1 = _thing1 == _thing3;

NSLog(@"thing1 after %li",(long)_thing1);
NSLog(@"thing2 after %li",(long)_thing2);
NSLog(@"thing3 after %li",(long)_thing3);

I have only been getting a 1 or 0 for _thing1. Does this mean its just checking the equality?

Upvotes: 1

Views: 334

Answers (2)

Wyetro
Wyetro

Reputation: 8588

You are just assigning the value of the inequality. _thing1 is equal to if _thing1 is equal to _thing3.

For example:

int aa = 1;
int bb = 2;
int cc = 3;
NSLog(@"%d", aa); // 1 (the value that was set for aa)
aa = aa == b;
NSLog(@"%d", aa); // 0 - which means false (1 means true)

Upvotes: 0

Bryan Chen
Bryan Chen

Reputation: 46578

it is equal to

int temp = _ting1 == ting3;
_thing1 = temp;

so _thing1 will be 1 (YES) if they are equal and 0 (NO) otherwise


another way to write _var1 = _var2 == nil; is _var1 = !_var2; some people think second way is more readable (including me) and some prefer first way.

Upvotes: 1

Related Questions