Assign this or that in Objective-C

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Objective-C

In Objective-C, you can assign values to variables using the assignment operator =. Here's an example:

NSString *name = @"John";

In the above example, we are assigning the string value "John" to a variable named name, which is of type NSString.

You can also use conditional statements to assign values based on certain conditions. Here's an example:

NSInteger a = 10;
NSInteger b = 20;

NSInteger max;

if (a > b) {
max = a;
} else {
max = b;
}

In the above example, we are assigning the value of either a or b to the variable max, depending on which one is greater. If a is greater than b, we assign a to max. Otherwise, we assign b to max.

You can also use the ternary operator to write this code more concisely:

NSInteger max = (a > b) ? a : b;

In this example, we are assigning the value of either a or b to the variable max based on the condition a > b. If the condition is true, we assign a to max. Otherwise, we assign b to max.