Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - Is !!BOOL Beneficial

I'm looking over the diffs submitted to a project by another developer, and they have a lot of code that does !!<some BOOL value>. In fact, this seems to be their standard pattern for implementing boolean getters and setters. They've implemented their code like:

- (BOOL) hasId {
    return !!hasId_;
}
- (void) setHasId:(BOOL) value {
    hasId_ = !!value;
}

I've never seen this pattern before, and am wondering if there is any benefit in using it. Is the double-negation doing anything useful?

like image 838
aroth Avatar asked Jul 12 '11 23:07

aroth


1 Answers

The double boolean operator just makes sure that the value returned is either a 1 or a 0. That's all : )

like image 175
citizen conn Avatar answered Oct 05 '22 23:10

citizen conn