Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding method returning BOOL by using !=

This is a rather basic question regarding the syntax of the return statement in the shouldAutoRotateToInterfaceOrientation method of a view controller.

In order to allow all views except for the upside-down portrait mode, I have the following chunk of code implemented:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

What exactly is the return statement doing? I understand that it is returning a boolean variable, but how is it determining whether to return true or false? Is this a kind of implicit if statement inside of the return statement? I.e. would:

-    (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown)
        return YES;
}

technically be the same thing, just more explicitly stated?

Thanks for the clarification!

like image 547
Joe Tyren Avatar asked Jan 19 '23 18:01

Joe Tyren


1 Answers

The result of a comparison like (something != something_else) is a BOOL value. If the comparison is true, the expression (....) takes the value YES (which is the same as TRUE).

It isn't an implicit conversion, it is just how comparisons work.

like image 171
James Avatar answered Jan 29 '23 14:01

James