同一行上的赋值和比较运算符

时间:2014-09-05 04:25:47

标签: objective-c

我试图弄清楚这是做什么或者什么是简写?

_var1 = _var2 == nil;

我一直在测试

_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);

我只得到_或1的1或0。这是否意味着它只是检查平等?

2 个答案:

答案 0 :(得分:1)

等于

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

所以_thing1如果相等则为1(是),否则为0(否)


另一种写_var1 = _var2 == nil;的方式是_var1 = !_var2;有些人认为第二种方式更具可读性(包括我),有些人更喜欢第一种方式。

答案 1 :(得分:0)

您只是指定不等式的值。 _thing1等于if _thing1等于_thing3。

例如:

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)