Short IF ELSE syntax in objective C
40,333
Solution 1
Yes.
Example (pseudo):
value = (expression) ? (if true) : (if false);
Based on your example (valid code):
BOOL result = value ? YES : NO;
Solution 2
It's exactly the same in both languages, except you typically don't find $
signs in Objective-C variable names.
if(value)
return 1;
else
return 0;
return value?1:0;
You should also keep in mind that the conditional operator ?:
isn't a shorthand for an if-else statement so much as a shorthand for a true vs false expression. See the PHP manual.
Solution 3
Surprised that nobody has suggested the following :
-
Long version :
if(value) return 1; else return 0;
-
Small version :
return value;
And if value
isn't a bool
variable, just cast it : return (BOOL)value;
Author by
Firdous
Updated on September 01, 2020Comments
-
Firdous over 3 years
Is there any short syntax for if-else statement in objective C like PHP:
if($value) return 1; else return 0;
shorter version:
return $value?1:0;
-
Bryan Chen over 10 years
return value?1:0;
is same asreturn !!value;
-
Hot Licks over 10 yearsNo, there is no such thing in Objective-C itself. But Objective-C is built on C and has full access to C syntax/semantics, and the exact same syntax is present in C, C++, Java, and several other languages. PHP no doubt got the construct from C.
-
Regexident over 9 yearsFYI, the
? :
-thing is called a ternary operator. -
holex over 9 yearsas long as the PHP is based on syntax of ANSI-C-language, and the ObjectiveC is a subset of the ANSI-C-language, and the ternary operator is available in ANSI-C already – this question is totally stupid and would not have deserved even a single upvote.
-
Firdous over 9 years@holex demand my friend!
-
holex over 9 years@Firdous, that 'demand' is based on that fact, where most of the developers have no idea about the languages / operating systems and they are not interested in learning more about those either – and that is really good in my view, because as long as such question can be raised and upvoted 19 times(!) by a strict developer community, none of the companies will question the professionals' rates; they simply demand them and such questions just push our hourly rate up to the sky; it is brilliant, isn't it? :)
-
Firdous over 9 years@holex so how many hourly rate u managed to get after this question? :)
-
holex over 9 years@Firdous, here I'm doing it for fun and free; contact me in private if you have a particular task to commit it and I will tell my rate to you. ;)
-
-
atomkirk about 10 years?: is available in C and kids. It means "this or that if this is false".
int i = j ?: k;
If j is 0, k will be assigned. -
BoltClock about 10 years@atomkirk: Interesting, so it does exist outside of PHP (and works identically). I removed my erroneous statement.
-
Regexident over 9 yearsThat's because the latter (small version) is just wrong. For old ObjC runtimes and compilers at least. On those
BOOL
is atypedef
onsigned char
. As such any value with a value larger than 255 with the smaller byte all zeros (such as256
) would result inNO
, instead ofYES
, as casting to a single-bytesigned char
would strip the higher, non-zero bits away. Doreturn !!value;
instead, orreturn value != NO;
. More: nshipster.com/bool -
siburb over 9 yearsThe other benefit of ?: is that "value" is only evaluated once if for example you want to do something like: "if value return value else return nil"
-
Sergey Lost almost 9 yearsThis makes my code so nice, tidy and sophisticated. Thanks!
-
Septronic about 8 yearsI used it like this and worked perfectly: NSString *toRet = [dataToSave writeToFile:filePath atomically:NO]? @"Yes" : @"No";