Reputation: 199
Sorry if something like this has been asked but I thought I understood booleans and this has me confused. In a command-line obj-c program the BigNerdRanch book says that the main function returns 0 which in boolean means false and that is a good thing because if it returns true or 1 than that means an error has occured.
so this code here later on in the book has me confused.
-(BOOL)readFromData (NSData *)data ofType:(NSString *)typeName error(NSError **)outError
todoItems = [NSPropertyListSerialization propertyListWithData: data
options:NSPropertyListMutableContainers
format:NULL
error:outError
return (todoItems != nil);
wouldn't this code say basically that since todoItems is going to be loaded with data it's pointer won't be set to nil therefore it will evaluate true which would return true as the BOOL value of the method meaning there was an error? I'm so confused by this.
Upvotes: 2
Views: 460
Reputation: 318954
Don't confuse the semantics of methods that return a BOOL with the main
function returning an int
.
In C, many functions return an int
value indicating success or failure. 0
usually means success while a non-zero (not just 1
) is an error code of some sort. The code can be just about any non-zero number. That's just a convention that has been used since the 70's.
When you write a method that returns a BOOL
, you are not dealing with an error code. It's either YES
or NO
. NO
usually means it failed while YES
means it succeeds.
The fact that the value NO
is really 0
has nothing at all to do with the old standard C convention of using 0
to indicate success. Don't confuse their meanings.
Read the use of a BOOL
value like reading a sentence.
if ([someClass readFromData...]) {
}
Read this like it looks. A success value of YES
means it worked and reading the code makes sense.
Upvotes: 3
Reputation: 56
The main() function returning zero does, indeed, by convention mean that the program has exited cleanly. You would return non-zero values from main() if there were errors. These error values could have program specific-meaning, or could be standard POSIX error values.
For other functions that return BOOL values, it's up to the implementor to indicate whether a YES means success or failure (or the opposite for NO). In this case -readFromData:ofType:error: returns YES if there's a non-nil set of data. I don't have the book, but I presume that means YES is intended to be mapped to success and NO is mapped to failure.
In other words, the convention for the return value from main() does not necessarily correlate to the the convention for the return value from a function that returns a BOOL, because there is not a convention for the latter.
Upvotes: 0
Reputation: 703
return (todoItems != nil);
basically means that if todoItems
is nil
, then the expression is evaluated to return false;
and if todoItems
is not nil
, then it would be return true;
Think about it as a compact if-statement.
EDIT: Sorry for my missunderstanding of your question. But I actually think that the author of the book is trying to give you an example of how you can use the return value to indicate an error. The loadfunction is probably returning nil
when there was an error, and thus, the the program would exit with return true
, which is an error.
Upvotes: 0