2

I know setting a variable conditionally is valid:

someProperty = [anObject aFunctionThatReturnsBool]? @"Yes" : @"No";

This question has an answer that details the use of ternary statements in preprocessor macros

#define statusString (statusBool ? @"Approved" : @"Rejected")

and within a string formatting method

[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]

But what about within any method?

[NSNumber numberWithInt:(aVariableThatCouldBeSet)? 100 : 0)];

And conditional method calling?

[anObject aFunctionThatReturnsBool]? [self doThis] : [self doThat];

Bonus points for any other uses not listed.

Community
  • 1
  • 1
user
  • 3,388
  • 7
  • 33
  • 67
  • 2
    @bengoesboom - sorry, but that is wrong. The ternary operator is used in *expressions* and has a *value*. The `if` construct is a *statement*, it does not have a value but determines whether other statements are executed. – CRD Sep 05 '13 at 03:58
  • @CRD you are correct. i didn't actually write what i meant (and what I wrote is just wrong). I meant to say it is like calling the function `f(x,y,z) { if (x) return y; else return z;}` – bengoesboom Sep 05 '13 at 04:07
  • Another way that the `?:` operator is different from `if/else` is that with the `?:` operator, both expressions on either side of the colon must result in two compatible values. You can call two methods but both methods must have non-void return values and the two return values must be of the same (or compatible) type. – rmaddy Sep 05 '13 at 04:08
  • @user - the conditional ternary operator (`p ? a : b`) is an *expression* which delivers an *value* - to be more precise in C-ese it delivers an *rvalue*. The types of the sub-expressions `a` & `b` must be compatible (there is a precise definition of this, but it means what it seems) and this is the type of the returned value. The operator may be used anywhere a value of the type it returns is required. In an assignment statement to a variable (`v = e`) the variable is being used as an *lvalue* - so you cannot use a conditional operator to select between two variables to assign to. HTH. – CRD Sep 05 '13 at 04:09
  • @RobertHarvey - Surely its not really too broad; expressions, statements, lvalues and rvalues can almost be covered in a comment, or two... ;-) – CRD Sep 05 '13 at 04:16
  • 1
    "are there other tricks ternary statements..." There's no question to answer here, just a list request. – Robert Harvey Sep 05 '13 at 04:18
  • So my question is invalid because the answer involves a list? Albeit a very short and specific list. I appreciate the guidelines that keep up the quality on this site, but putting this on hold is quite excessive. – user Sep 05 '13 at 04:23
  • @RobertHarvey- changed the question to be narrow and of use to a smaller crowd. – user Sep 05 '13 at 04:35
  • @user - continuing from last comment and taking your examples in order: `#define` - macros are just *text replacement* so you can put anything in them, in this case you have an expr of type `NSString *` so you can use it wherever a value of that type is required; `stringWithFormat:` & `numberWithInt:` - here you are passing an expr as an argument to a method so that's fine; conditional method calling - as long as the two methods return compatible types then this is fine. Surprisingly if both methods are declared `void` this is also fine, but it is better to use an `if` statement in that case. – CRD Sep 05 '13 at 06:54
  • @bengoesboom - it is like calling your `f(x,y,z)` function *except* that only one of `y` & `z` is evaluated - so if either have side-effects the function is not the same as the operator (or the `if` statement) – CRD Sep 05 '13 at 07:00
  • I still don't see a question here. Is your question really "what about this?" What about it? Did the code do what you expected it to do? – Robert Harvey Sep 05 '13 at 16:20
  • Well it lost a lot of value when I tried to make it more exacting. What I want to know: "What lesser-known ways can I use a ternary operator in Objective-C"? but I can't explicitly say that because this question could arbitrarily be labeled too broad. A succinct and empirical answer is very within reach. If this question bothers you, please act democratically and request it be moved to a better location. It's a fantastic question with a juicy answer awaiting. Please don't spoil it. – user Sep 05 '13 at 19:27
  • 2
    user - there is no "juicy answer awaiting", what you need to know is in the comments (as no answer is possible, and I disagree with @RobertHarvey on that). `?:` is just an *operator* and can be used wherever an *expression* is allowed. It only evaluates the operands it needs to, something it shares with the logical operators (`&&` & `||`). It has a special case in that its 2nd & 3rd operands can be of `void` type - your conditional method calling example - one of the few operators to accept operands of type `void` (the comma operator's first operand can be void). That's it. HTH. – CRD Sep 05 '13 at 22:02
  • That does help, CRD. But I would still enjoy helpful examples (which I believe is the taboo part of this question), and I'm sure others would too. Like using ternaries in the preprocessor statement. You can *tell me* I can use it wherever an expression is allowed, but seeing the preprocessor example helps me better sink in the information. It's a bummer asking for examples is so frowned upon. – user Sep 05 '13 at 22:30
  • @user - I think you may be thinking about this the wrong way. Try this: when you need to provide a value (expression), of any type, and wish to know if you can use the `?:` to make a choice between two values (sub-expressions) ask yourself "If what was needed here was an integer could I write `a + b` (or `(a + b)` to be sure of association)?" - if you answer yes then you can use `?:` (or `(?:)`) there as well. That's it. No special handshake required ;-) – CRD Sep 06 '13 at 01:50
  • @CRD - Thanks, your explanation gives me a pretty good grip on the subject to explore on my own. I guess I'll just make a blog post with the answer I was expecting originally (Since I can't write it here). Maybe someone will find it nifty. – user Sep 06 '13 at 02:20

0 Answers0