2014-09-05 57 views
1

我想知道這是做什麼或它是什麼速記?在同一行上的賦值和比較運算符

_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或0 _thing1。這是否意味着它只是檢查平等?

回答

1

它等於

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

所以_thing1將是1(YES),如果他們是平等的0(無),否則


另一種方式來寫_var1 = _var2 == nil;_var1 = !_var2;有些人認爲第二種方式更具可讀性(包括我),有些更喜歡第一種方式。

0

您只是分配不等式的值。如果_thing1等於_thing3,_thing1等於。

例如:

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)