Search code examples
objective-cconditional-statementsampersand

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] ) {
}

Solution

  • 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) ...