Return 1; ...just wondering?


#1

In the ImageFetch project, I pretty much understand every line of this code except for return 1 as it appears in its natural habitat below…

if (!written) { NSLog(@"write failed: %@", [error localizedDescription]); return 1; }

Why is this here? Why is it returning 1? Who is it returning 1 to? What do they do with that 1? I don’t want to ride the pony; the other kids will laugh at me.


#2

In Unix world, thus in Mac OS world, the convention is to return 0 to the OS to indicate normal completion, and a nonzero value to indicate an abnormal completion.

For example:


bool StartFoo ();

int main (int nargs, char *argv [])
{
      if (StartFoo ())
           return 0;    // started
      else
           return 1;    // did not start
}

The operating system will make this information, the value returned by main (), available to other programs if they ask for it.

Your code fragment:

if (!written) {
   NSLog(@"write failed: %@", [error localizedDescription]);
   return 1;
}

is returning a nozero value to signal that the write operation has failed.


#3

It also seems to be telling the NSLog that’s outside the if {…} not to print…

That seems looks a bit wierd to me, as we didn’t put the NSLog in } else { as we usually do…

…feliz fin de semana!!


#4

Earlier in the book, the return 0 and return 1 lines of code was discussed, along with the EXIT_SUCCESS or EXIT_FAILURE used to get rid of the magic numbers, but then oddly the book continues using the magic numbers (??!!??).

It is always better to NOT use magic numbers when an alternative is available (increases understanging of the code, and allows for compiler changes, increases portability, etc).