What is the mean of the ampersand symbol(&) in this sentence?

if ([_managedObjectContext hasChanges] & ![_managedObjectContext save:&error] ) {
}

I always use && in conditional sentences, today suddenly see there is only & in the above sentence, what is the difference between them in this sentence?

Update : the code above is wrong , it just a typo, sorry, it should use &&, not &, and look like this:

if ([_managedObjectContext hasChanges] && ![_managedObjectContext save:&error] ) {
}

It has been explained what is the difference between & and &&, but the code is actually a bug:

save: will be called, whether hasChanges is true or not. That is very strange - when there are no changes, there is no need to call save:. If there is actually code between the curly brackets, that code would be executed if there are changes and save: fails. If there are no changes, and save: fails for any reason, then the failure would not be detected. If there is no code between the curly braces, there is no need to call hasChanges at all if save: is going to be called anyway.

Since it is completely weird what the code does, and there are no comments why the code is weird, it is a bug.

In general, using & for logic is highly suspicious. & should be used for bitwise and, like

if ((value & mask) != 0) ...

If you really want a "logical and" and evaluate both halves (for example because they have side effects), you would write

BOOL hasChanges = [_managedObjectContext hasChanges];
BOOL saveFailed = ! [_managedObjectContext save:&error];

if (hasChanges && saveFailed) ...

& is a bitwise AND operator. Unlike logical && , it

  • Produces values based on all bits of the two operands, and
  • Does not short-circuit the evaluation.
  • In this particular case the fact that the operator operates on all bits is not relevant, because the second operand has ! in front of it. This is a logical NOT, so its result is going to be either 1 or 0 .

    However, the absence of short-circuiting is relevant: there would be a call to the [_managedObjectContext save:&error] method, even if [_managedObjectContext hasChanges] returns zero.

    链接地址: http://www.djcxy.com/p/72654.html

    上一篇: 二进制AND运算符在If语句条件中

    下一篇: 这句话中&符号(&)的含义是什么?