- Computers & Software»
- Computer Science & Programming
Objective-C Programming| The Conditional operator ? : | Ternary
Conditional operators are very obscure. Many programmers would like to use them only they can’t remember what they are called. This type of operator uses two symbols; the question “?” and the colon “:” to express an If...else expression. Original introduced with the C programming language, they ave been implemented by all major languages including Objective-C as this language is a superset of the C language, all of the C programming is at the core of the Objective-C language. Conditional operators are also known as ternary operators.
Conditional operator are very useful for performing for inline if else operations. The general syntax is:
expression_to-evaluate ? expression_if_true : expression_if_false
Conditional expressions can also be used with instance variables to assign a value based on a conditional expression:
instance_variable = expression_to-evaluate ? expression_if_true : expressioon_if_false
You could also method with a return type to evaluate and expression:
instance_variable = method_with_return_type ? return_true_value : return_false_value
Conditional or ternary operators are easy and versatile to use as these examples demonstrate, thus:
To reproduce this next example, create an Empty Xcode project and uncheck the Core Data option:
Once the project is created, add another NSObject subclass object where we will add the Conditional operator sample code in a method. The method can be named anything, so I will call my isAnything. Another BOOL variable (not an instance variable), this will be called isMyName.
Once these elements are created in the header file, open the implementation file to synthesize the accessor property myName and implement the isAnything method. In the isAnything method, initialize the myName instance variable and assign a NSString value.
Next, test to the see if the value of the myName instance variable is indeed the value in the test condition. If it is, return TRUE otherwise return FALSE and assign this value to the isMyName variable.
In the above screenshot, the condition myName == @"Kevin" is tested. If the Boolean expression is true, then the TRUE value is retuned otherwise the FALSE value is returned.
The final example demonstrates using a method with a Boolean type as the test condition. Back in the header file add another method: testMyName with a return Boolean object type. Save and it. In the implementation file add an if myName == @”Kevin” and return True or False like in the screenshot below:
If you try to use the testMyName method without first declaring a new ConditionalTest object you will get a compiler complaint as seen below:
So it is important to create a new object, in our case, a ConditionalTest object and then you can call the method in the Conditional operation and return the appropriate value.
As you have seen in examples and the explanation above, the Conditional or Ternary operator is quite powerful and handy for inline Boolean expression testing.