Blog

  • Numbers in Objective-C

    Numbers in detail

    In Objective-C, numbers are treated as objects instead of primitive data types like in other programming languages. This allows for a greater range of values and offers various methods to work with numbers through the NSNumber class. These methods make number handling in Objective-C more flexible and powerful.

    Instead of using primitive data types such as intfloat, and double, Objective-C allows these types to be encapsulated as objects, providing additional functionality. Using NSNumber, you can perform a variety of mathematical operations and store numbers as objects, which can then be manipulated and returned using specific methods.

    Example 1: Addition of Two Numbers

    In this example, we create an NSNumber object for each number and then add them together. The result is also stored as an NSNumber object.

    #import <Foundation/Foundation.h>
    
    @interface MathOperations:NSObject
    - (NSNumber *)addNumber:(NSNumber *)a withNumber:(NSNumber *)b;
    @end
    
    @implementation MathOperations
    
    - (NSNumber *)addNumber:(NSNumber *)a withNumber:(NSNumber *)b {
       float num1 = [a floatValue];  // Extract the first number as a float
       float num2 = [b floatValue];  // Extract the second number as a float
       float sum = num1 + num2;     // Calculate the sum
       NSNumber *result = [NSNumber numberWithFloat:sum];  // Store the sum in an NSNumber
       return result;  // Return the result
    }
    
    @end
    
    int main() {
       @autoreleasepool {
           MathOperations *operations = [[MathOperations alloc] init];
           NSNumber *num1 = [NSNumber numberWithFloat:15.5];  // First number
           NSNumber *num2 = [NSNumber numberWithFloat:9.0];   // Second number
           NSNumber *sum = [operations addNumber:num1 withNumber:num2];  // Add the numbers
           NSString *sumString = [sum stringValue];  // Convert the result to a string for display
           NSLog(@"The sum is %@", sumString);  // Output the result
       }
       return 0;
    }

    Components of a For Loop:

    1. Initialization: Used to initialize counters or iterators (e.g., int counter = 1;).
    2. Condition: Determines how long the loop will run (e.g., counter <= 10;).
    3. Updation: Updates the counter or iterator after each iteration (e.g., counter++;).

    Examples:

    The sum is 24.5
    Methods for Working with Numbers in Objective-C

    The NSNumber class provides several methods that make working with numbers easier. These include:

    • (BOOL)boolValue: Converts the NSNumber to a boolean.
    • (float)floatValue: Converts the NSNumber to a float.
    • (NSString *)stringValue: Converts the NSNumber to a NSString.
    • (int)intValue: Converts the NSNumber to an int.
    • (NSNumber *)numberWithFloat:(float)value: Creates an NSNumber object with a float value.

    Objective-C also includes several mathematical functions, such as:

    • CEIL(): Returns the smallest integer greater than or equal to the given number.
    • COS(): Returns the cosine of a given number.
    • FLOOR(): Returns the largest integer less than or equal to the given number.
    • MOD(): Returns the remainder of dividing two numbers.
    • RAND(): Generates a random number.

    Example 2: Subtracting Two Numbers

    In this example, we will subtract two numbers, 100 and 40, and display the result.

    #import <Foundation/Foundation.h>
    
    @interface MathOperations:NSObject
    - (NSNumber *)subtractNumber:(NSNumber *)x fromNumber:(NSNumber *)y;
    @end
    
    @implementation MathOperations
    
    - (NSNumber *)subtractNumber:(NSNumber *)x fromNumber:(NSNumber *)y {
       float num1 = [x floatValue];  // Extract the first number as a float
       float num2 = [y floatValue];  // Extract the second number as a float
       float result = num1 - num2;   // Calculate the difference
       NSNumber *resultObj = [NSNumber numberWithFloat:result];  // Store the result as an NSNumber
       return resultObj;  // Return the result
    }
    
    @end
    
    int main() {
       @autoreleasepool {
           MathOperations *operations = [[MathOperations alloc] init];
           NSNumber *num1 = [NSNumber numberWithFloat:100.0];  // First number
           NSNumber *num2 = [NSNumber numberWithFloat:40.0];   // Second number
           NSNumber *difference = [operations subtractNumber:num1 fromNumber:num2];  // Subtract the numbers
           NSString *resultString = [difference stringValue];  // Convert the result to a string
           NSLog(@"The result is %@", resultString);  // Output the result
       }
       return 0;
    }

    Output:

    The result is 60
  • Blocks in Objective-C

    Blocks in Objective-C are a powerful tool that help simplify development by allowing you to write more concise and efficient code. Blocks are self-contained pieces of code that can be passed around and executed as objects. They are widely used in Objective-C for purposes such as callbacks, iterators, and passing data between objects.

    Blocks in Objective-C are similar to functions in that they take arguments, execute code, and return a value. However, they also have the added benefit of being assignable to variables and passed as arguments to methods. Blocks can access and modify variables in the scope in which they are defined, making them highly flexible.

    Types of Blocks

    1. Standard Blocks:

    Standard blocks are simple blocks that are declared using the ^ symbol. These blocks can be used to control the flow of execution, such as executing multiple pieces of code in sequence.

    Syntax of Standard Block:

    ^(int a, int b) {
        int c = a + b;
        return c;
    }
    2. Typed Blocks:

    Typed blocks are blocks with a specified return type and argument types. These blocks allow you to define behavior for specific types of data.

    Syntax of Typed Block:

    ^int(int a, int b) {
        int c = a + b;
        return c;
    }
    3. Autoreleasing Blocks:

    Autoreleasing blocks are blocks that are declared using the __autoreleasing keyword. These blocks are automatically released when they go out of scope, helping with memory management.

    Syntax of Autoreleasing Block:

    @autoreleasepool {
        ^int(int a) {
            int b = a + 1;
            return b;
        }
    }

    Example 1:

    // Standard Block
    int (^multiply)(int, int) = ^(int num1, int num2) {
        return num1 * num2;
    };
    int result = multiply(3, 5); // result is 15
    
    // Typed Block
    typedef int (^AdditionBlock)(int, int);
    AdditionBlock add = ^(int num1, int num2) {
        return num1 + num2;
    };
    int sum = add(3, 5); // sum is 8
    
    // Autoreleasing Block
    @autoreleasepool {
        NSString *string = @"Hello, world!";
        void (^printString)(void) = ^{
            NSLog(@"%@", string);
        };
        printString();
    }

    Output:

    Hello, world!

    In this example:

    • The multiply block is a standard block that takes two integer parameters and returns their product.
    • The add block is a typed block that is declared using typedef and takes two integers as parameters and returns their sum.
    • The printString block is an auto-releasing block, declared within an @autoreleasepool statement, that prints a string to the console.

    Example 2:

    #import <Foundation/Foundation.h>
    
    // Block with no parameters and no return value
    typedef void (^SimpleBlock)(void);
    
    // Block with one parameter and no return value
    typedef void (^ParameterBlock)(NSString *);
    
    // Block with no parameters and a return value
    typedef int (^ReturnBlock)(void);
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // SimpleBlock implementation
            SimpleBlock simpleBlock = ^{
                NSLog(@"Welcome to the World of Blocks!");
            };
            simpleBlock(); // Output: "Welcome to the World of Blocks!"
    
            // ParameterBlock implementation
            ParameterBlock parameterBlock = ^(NSString *name) {
                NSLog(@"Hello, %@!", name);
            };
            parameterBlock(@"Alice"); // Output: "Hello, Alice!"
    
            // ReturnBlock implementation
            ReturnBlock returnBlock = ^{
                return 42;
            };
            int result = returnBlock();
            NSLog(@"The result is %d", result); // Output: "The result is 42"
    
            // Addition Block implementation
            int (^AdditionBlock)(int, int) = ^(int a, int b) {
                return a + b;
            };
            int sum = AdditionBlock(3, 5);
            NSLog(@"The addition of 3 and 5 is %d", sum); // Output: "The addition of 3 and 5 is 8"
        }
        return 0;
    }

    Output:

    Welcome to the World of Blocks!
    Hello, Alice!
    The result is 42
    The addition of 3 and 5 is 8

    Explanation:

    1. SimpleBlock: A block with no parameters and no return value that outputs a simple message.
    2. ParameterBlock: A block that takes a string parameter and outputs a greeting message.
    3. ReturnBlock: A block that returns an integer value.
    4. AdditionBlock: A block that takes two integers as parameters, adds them together, and returns the sum.

  • Functions

    Defining and Calling Functions

    In Objective-C, functions are defined outside of any class or method and can be called from anywhere in the program where they are visible. Functions can accept parameters and return values.

    Defining Functions

    A function definition includes the return type, function name, and parameters (if any).

    // Function definition
    int add(int a, int b) {
        return a + b;
    }
    Calling a Function

    To call a function, simply use its name followed by arguments in parentheses.

    // Function call
    int result = add(5, 3);  // result is 8
    NSLog(@"The result is %d", result);

    Function Parameters and Return Values

    Functions can accept parameters and return values of various data types.

    Function with Parameters

    You can define a function with parameters to pass values to it.

    // Function with parameters
    void printGreeting(NSString *name) {
        NSLog(@"Hello, %@", name);
    }
    
    // Calling the function with an argument
    printGreeting(@"Alice");
    Function with Return Value

    A function can return a value using the return statement.

    // Function with a return value
    double multiply(double x, double y) {
        return x * y;
    }
    
    // Calling the function and storing the return value
    double product = multiply(4.5, 2.3);
    NSLog(@"The product is %f", product);
    Function with No Parameters and No Return Value

    You can also define a function with no parameters and no return value.

    // Function with no parameters and no return value
    void sayHello() {
        NSLog(@"Hello, world!");
    }
    
    // Calling the function
    sayHello();

    Function Pointers

    Function pointers are used to store the address of a function, allowing you to call a function indirectly.

    Declaring a Function Pointer

    To declare a function pointer, specify the return type, followed by (*pointerName), and the parameter list.

    // Declare a function pointer
    int (*operation)(int, int);
    Assigning a Function to a Function Pointer

    Assign a function to a function pointer by specifying the function’s name without parentheses.

    // Use the function pointer to call the function
    int sum = operation(10, 5);  // Calls the 'add' function
    NSLog(@"The sum is %d", sum);

    Example: Using Function Pointers

    Here’s a complete example demonstrating the use of function pointers:

    #include <Foundation/Foundation.h>
    
    // Define a function
    int add(int a, int b) {
        return a + b;
    }
    
    int subtract(int a, int b) {
        return a - b;
    }
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // Declare a function pointer
            int (*operation)(int, int);
    
            // Assign functions to the function pointer and call them
            operation = add;
            NSLog(@"Add: %d", operation(10, 5));  // Outputs 15
    
            operation = subtract;
            NSLog(@"Subtract: %d", operation(10, 5));  // Outputs 5
        }
        return 0;
    }
  • Loops in Objective-C Loops

    In programming, there are scenarios where you need to execute a specific block of code multiple times. Typically, program statements are executed in a linear sequence, where the first statement is followed by the second, and so forth. However, programming languages provide control structures to manage more complex execution flows.

    loop statement allows a specific block of code to be executed repeatedly based on a condition. Below is the general structure of a loop statement, which is common across most programming languages: Types of Loops

    Types of Loops

    For Loop in Objective-C

    The for loop in Objective-C is a control structure used for repeating a set of instructions for a specified number of iterations. Unlike while or do-while loops, the for loop allows variable initialization, condition checking, and variable updates all in a single line.

    Syntax:

    for (initialisation_of_expression; condition_of_expression; update_expression)
    {
        // Body of the loop
        // Instructions to execute
    }

    Components of a For Loop:

    1. Initialization: Used to initialize counters or iterators (e.g., int counter = 1;).
    2. Condition: Determines how long the loop will run (e.g., counter <= 10;).
    3. Updation: Updates the counter or iterator after each iteration (e.g., counter++;).

    Examples:

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int counter;
    
        for (counter = 1; counter <= 10; counter++)
        {
            NSLog(@"%d. Hello, World!", counter);
        }
    
        [myPool drain];
        return 0;
    }

    Output:

    1. Hello, World!
    2. Hello, World!
    3. Hello, World!
    4. Hello, World!
    5. Hello, World!
    6. Hello, World!
    7. Hello, World!
    8. Hello, World!
    9. Hello, World!
    10. Hello, World!

    Example 2: Breaking Out of a For Loop

    The break statement allows exiting the loop before the condition becomes false.

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int counter;
    
        for (counter = 1; counter <= 10; counter++)
        {
            NSLog(@"%d. Processing...", counter);
    
            if (counter == 7)
            {
                NSLog(@"Breaking out of the loop...");
                break;
            }
        }
    
        [myPool drain];
        return 0;
    }

    Output:

    1. Processing...
    2. Processing...
    3. Processing...
    4. Processing...
    5. Processing...
    6. Processing...
    7. Processing...
    Breaking out of the loop...

    Example 3: Nested For Loops

    This program demonstrates how nested loops work.

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int i, j;
    
        for (i = 1; i <= 3; i++)
        {
            NSLog(@"Outer loop iteration: %d", i);
    
            for (j = 1; j <= 5; j++)
            {
                NSLog(@"  Inner loop iteration: %d", j);
            }
        }
    
        [myPool drain];
        return 0;
    }

    Output:

    Outer loop iteration: 1
      Inner loop iteration: 1
      Inner loop iteration: 2
      Inner loop iteration: 3
      Inner loop iteration: 4
      Inner loop iteration: 5
    Outer loop iteration: 2
      Inner loop iteration: 1
      Inner loop iteration: 2
      Inner loop iteration: 3
      Inner loop iteration: 4
      Inner loop iteration: 5
    Outer loop iteration: 3
      Inner loop iteration: 1
      Inner loop iteration: 2
      Inner loop iteration: 3
      Inner loop iteration: 4
      Inner loop iteration: 5

    Example 4: For Loop with Multiple Variables

    This example shows a for loop that initializes and updates two variables simultaneously.

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int i, j;
    
        for (i = 1, j = 10; i <= 10 && j >= 1; i++, j--)
        {
            NSLog(@"i = %d and j = %d", i, j);
        }
    
        [myPool drain];
        return 0;
    }

    Output:

    i = 1 and j = 10
    i = 2 and j = 9
    i = 3 and j = 8
    i = 4 and j = 7
    i = 5 and j = 6
    i = 6 and j = 5
    i = 7 and j = 4
    i = 8 and j = 3
    i = 9 and j = 2
    i = 10 and j = 1
    Do-While Loop in Objective-C

    Just like other programming languages, Objective-C supports the do..while loop. A do..while loop is also known as an inverted while loop because, in a while loop, the condition is checked before executing the loop body. If the condition is false, the loop does not execute. However, in a do..while loop, the condition is evaluated at the end of the loop body, ensuring the body executes at least once, regardless of the test condition.

    Syntax:

    do {
        // Loop Body
        // Update Statement
    } while(expression);

    Parts of a Do-While Loop:

    1. Loop Body: Contains the statements or logic to be executed repeatedly.
    2. Update Statement: Modifies the loop control variable (e.g., incrementing or decrementing).
    3. Expression: A condition evaluated after the loop body. If true, the loop continues; if false, the loop terminates.

    How the Do-While Loop Works:

    1. The loop body executes once when the do block is encountered.
    2. Statements in the loop body run.
    3. The update statement executes.
    4. The test condition is evaluated. If true, the control flow returns to Step 2.If false, the loop ends, and control exits the loop.

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int num = 8;
    
        // Do-While Loop
        do {
            // Loop Body
            NSLog(@"Loop is executing.");
    
            // Update Statement
            num++;
        } while (num < 10);
    
        [pool drain];
        return 0;
    }

    Output:

    Loop is executing.
    Loop is executing.

    Example 2:

    Objective-C program to print the multiplication table of 16:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int num = 1;
        int res;
    
        // Do-While Loop
        do {
            // Loop Body
            res = 16 * num;
            NSLog(@"16 * %d = %d", num, res);
    
            // Update Statement
            num++;
        } while (num <= 16);
    
        [pool drain];
        return 0;
    }

    Output:

    16 * 1 = 16
    16 * 2 = 32
    16 * 3 = 48
    16 * 4 = 64
    16 * 5 = 80
    16 * 6 = 96
    16 * 7 = 112
    16 * 8 = 128
    16 * 9 = 144
    16 * 10 = 160
    16 * 11 = 176
    16 * 12 = 192
    16 * 13 = 208
    16 * 14 = 224
    16 * 15 = 240
    16 * 16 = 256
    While Loop in Objective-C

    In Objective-C, the while loop allows for repeating a statement or a group of statements as long as the specified condition evaluates to true. Unlike other loops, the while loop checks its condition before executing the loop body. This makes it ideal for situations where the number of iterations is not known beforehand. If the condition is true, the loop executes; if the condition is false, the loop exits.

    while(condition) {
        // Body
    }

    How the While Loop Works:

    1. The program encounters the while loop.
    2. The condition is evaluated. If the condition is true, control enters the loop body. If the condition is false, the loop exits.
    3. After executing the loop body, any necessary updates occur.
    4. Control returns to the condition check. Steps 2–3 repeat as long as the condition remains true.
    5. When the condition becomes false, the loop ends, and control moves to the next part of the program.

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int num = 8;
    
        // While loop execution
        while(num < 10) {
            NSLog(@"Loop is running.");
            num++;
        }
    
        [pool drain];
        return 0;
    }

    Output:

    Loop is running.
    Loop is running.

    Explanation:

    1. The variable num is initialized with 8.
    2. The while loop checks the condition (num < 10). For num = 8, the condition is true, so the loop body executes, printing “Loop is running,” and num is incremented to 9.. For num = 9, the condition is still true, and the loop executes again.
    3. When num = 10, the condition becomes false, and the loop ends.

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int num = 1;
        int res;
    
        // While loop execution
        while(num <= 15) {
            res = 15 * num;
            NSLog(@"15 * %d = %d", num, res);
            num++;
        }
    
        [pool drain];
        return 0;
    }

    Output:

    15 * 1 = 15
    15 * 2 = 30
    15 * 3 = 45
    15 * 4 = 60
    15 * 5 = 75
    15 * 6 = 90
    15 * 7 = 105
    15 * 8 = 120
    15 * 9 = 135
    15 * 10 = 150
    15 * 11 = 165
    15 * 12 = 180
    15 * 13 = 195
    15 * 14 = 210
    15 * 15 = 225
    Nested loops in Objective-C

    Objective-C provides three fundamental repetition control structures, commonly known as loops. These loops enable programmers to repeatedly execute a single statement or a block of statements as long as a specified condition is met. Additionally, loops can be embedded within one another, a concept referred to as Nested Loops. Let’s explore them in detail.

    Objective-C supports three types of loops that can be nested:

    1. Nested While Loop
    2. Nested Do-While Loop
    3. Nested For Loop

    1. Nested While Loop: In Objective-C, a nested while loop occurs when one while loop is placed inside another. For every iteration of the outer loop, the inner loop executes completely, starting from its initial condition and continuing until its termination condition is satisfied.

    Syntax:

    // Outer while loop
    while (condition_expr_1)
    {
        // Inner while loop
        while (condition_expr_2)
        {
            // Statements executed by the inner loop
            statement_1;
    
            // Update the inner loop condition
            increment/decrement of condition_expr_2;
        }
    
        // Statements executed by the outer loop
        statement_3;
    
        // Update the outer loop condition
        increment/decrement of condition_expr_1;
    }

    Execution Flow of Nested While Loops

    Step 1: The control checks the condition of the outer while loop (condition_expr_1).

    Step 2: If condition_expr_1 is true:

    • The control enters the body of the outer loop.
    • The inner while loop condition (condition_expr_2) is checked.

    Step 3: The control enters the body of the outer loop. The inner while loop condition (condition_expr_2) is checked.

    Step 4: If condition_expr_2 is false, the control exits the inner loop and continues with the statements following it in the outer loop.

    Step 5: The outer loop condition (condition_expr_1) is updated and rechecked.

    Step 6: If condition_expr_1 is false, the control exits the outer loop and proceeds with the next statements in the program.

    Example:

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int i = 1;
        int j;
    
        // Outer while loop
        while (i <= 3)
        {
            // Message from outer while loop
            NSLog(@"%d. Message from outer while loop.", i);
    
            j = 1;
    
            // Inner while loop
            while (j <= 5)
            {
                // Message from inner while loop
                NSLog(@"    %d. Message from inner while loop.", j);
    
                // Update the inner while loop condition
                j++;
            }
    
            // Update the outer while loop condition
            i++;
        }
        [myPool drain];
        return 0;
    }

    Output:

    1. Message from outer while loop.
        1. Message from inner while loop.
        2. Message from inner while loop.
        3. Message from inner while loop.
        4. Message from inner while loop.
        5. Message from inner while loop.
    2. Message from outer while loop.
        1. Message from inner while loop.
        2. Message from inner while loop.
        3. Message from inner while loop.
        4. Message from inner while loop.
        5. Message from inner while loop.
    3. Message from outer while loop.
        1. Message from inner while loop.
        2. Message from inner while loop.
        3. Message from inner while loop.
        4. Message from inner while loop.
        5. Message from inner while loop.

    2. Nested Do-While Loop: Similar to a while loop, Objective-C allows you to nest two or more do-while loops within a program. The primary distinction between a while loop and a do-while loop is that a do-while loop executes its body at least once before checking its condition. In the case of nested loops, both the outer and inner do-while loops execute their bodies at least once, following the same basic logic as the while loop.

    Syntax

    // Outer do-while loop
    do
    {
        // Inner do-while loop
        do
        {
            // Statements executed by the inner do-while loop
            statement_1;
    
            // Update the inner do-while loop condition
            increment/decrement of condition_expr_2;
    
        } while (condition_expr_2);
    
        // Statements executed by the outer do-while loop
        statement_3;
    
        // Update the outer do-while loop condition
        increment/decrement of condition_expr_1;
    
    } while (condition_expr_1);

    Execution Flow of Nested Do-While Loops

    Step 1: The control enters the body of the outer do-while loop.
    Step 2: The control then enters the body of the inner do-while loop.
    Step 3: Execute the body of the inner loop.
    Step 4: Update the condition variables of the inner loop.
    Step 5: Check the condition of the inner do-while loop: If true it return to Step 2 and if false: Exit the inner loop and move to the next statements in the outer loop.
    Step 6: Update the condition variables of the outer loop.
    Step 7: Check the condition of the outer do-while loop. If condition is true then it returns to Step 1.If false: Exit the outer loop.

    Example:

    #import <Foundation/Foundation.h>
    
    int main ()
    {
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
    
        int i = 1;
        int j;
    
        // Outer do-while loop
        do
        {
            // Message from outer do-while loop
            NSLog(@"%d. Message from outer do-while loop.", i);
    
            j = 1;
    
            // Inner do-while loop
            do
            {
                // Message from inner do-while loop
                NSLog(@"    %d. Message from inner do-while loop.", j);
    
                // Update the inner loop condition
                j++;
    
            } while (j <= 5);
    
            // Update the outer loop condition
            i++;
    
        } while (i <= 3);
    
        [myPool drain];
        return 0;
    }

    Output:

    1. Message from outer do-while loop.
        1. Message from inner do-while loop.
        2. Message from inner do-while loop.
        3. Message from inner do-while loop.
        4. Message from inner do-while loop.
        5. Message from inner do-while loop.
    2. Message from outer do-while loop.
        1. Message from inner do-while loop.
        2. Message from inner do-while loop.
        3. Message from inner do-while loop.
        4. Message from inner do-while loop.
        5. Message from inner do-while loop.
    3. Message from outer do-while loop.
        1. Message from inner do-while loop.
        2. Message from inner do-while loop.
        3. Message from inner do-while loop.
        4. Message from inner do-while loop.
        5. Message from inner do-while loop.

  • Operators in Objective C

    The Objective-C language, built on top of C, retains many of the same operators as C. These operators are crucial for creating mathematical expressions and performing various operations. Operators act on variables (operands) to produce a result. For example, in the expression c = a + b, the + and = are operators, and ab, and c are operands.

    Objective-C is widely used for developing iOS and macOS applications. The operators can be categorized as follows:

    Types of Operators

    1. Arithmetic Operators: Arithmetic operators perform basic mathematical operations on operands, such as addition, subtraction, multiplication, and division.

    OperatorDescriptionExample
    +Adds two operandsc = a + b; // c = 12
    -Subtracts second operand from the firstc = a - b; // c = -2
    *Multiplies two operandsc = a * b; // c = 35
    /Divides numerator by denominatorc = a / b; // c = 4
    %Returns remainder of divisionc = a % b; // c = 0
    ++Increments operand by 1a++; // a = 6
    --Decrements operand by 1a--; // a = 5

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int n1 = 5, n2 = 3;
        NSLog(@"ADDITION = %d", (n1 + n2));
        NSLog(@"SUBTRACTION = %d", (n1 - n2));
        NSLog(@"MULTIPLICATION = %d", (n1 * n2));
        NSLog(@"DIVISION = %d", (n1 / n2));
        NSLog(@"MODULUS = %d", (n1 % n2));
    
        [pool drain];
        return 0;
    }

    Output:

    ADDITION = 8
    SUBTRACTION = 2
    MULTIPLICATION = 15
    DIVISION = 1
    MODULUS = 2

    2. Relational (Comparison) Operators: Relational operators compare two values, returning true or false based on the comparison.

    OperatorDescriptionExample
    ==Checks if two operands are equal(a == b) // false
    !=Checks if two operands are not equal(a != b) // true
    >Checks if left operand is greater(a > b) // true
    <Checks if left operand is smaller(a < b) // false
    >=Checks if left operand is greater or equal(a >= b) // true
    <=Checks if left operand is smaller or equal(a <= b) // false

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int n1 = 5, n2 = 3;
        NSLog(@"ADDITION = %d", (n1 + n2));
        NSLog(@"SUBTRACTION = %d", (n1 - n2));
        NSLog(@"MULTIPLICATION = %d", (n1 * n2));
        NSLog(@"DIVISION = %d", (n1 / n2));
        NSLog(@"MODULUS = %d", (n1 % n2));
    
        [pool drain];
        return 0;
    }

    Output:

    ADDITION = 8
    SUBTRACTION = 2
    MULTIPLICATION = 15
    DIVISION = 1
    MODULUS = 2

    3. Relational (Comparison) Operators: Relational operators compare two values, returning true or false based on the comparison.

    OperatorDescriptionExample
    ==Checks if two operands are equal(a == b) // false
    !=Checks if two operands are not equal(a != b) // true
    >Checks if left operand is greater(a > b) // true
    <Checks if left operand is smaller(a < b) // false
    >=Checks if left operand is greater or equal(a >= b) // true
    <=Checks if left operand is smaller or equal(a <= b) // false

    Examples:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int n1 = 5, n2 = 3;
        NSLog(@"Equal = %d", (n1 == n2));
        NSLog(@"NOT Equal = %d", (n1 != n2));
        NSLog(@"LESS THAN = %d", (n1 < n2));
        NSLog(@"GREATER THAN = %d", (n1 > n2));
    
        [pool drain];
        return 0;
    }

    Output:

    Equal = 0
    NOT Equal = 1
    LESS THAN = 0
    GREATER THAN = 1

    4. Logical Operators: Logical operators perform logical operations and return either true (1) or false (0).

    OperatorDescriptionExample
    &&Called Logical AND operator. If both the operands are non zero then condition becomes true.(A && B) is false.
    ||Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.(A || B) is true.
    !Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.!(A && B) is true.

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int n1 = 1, n2 = 0;
        NSLog(@"LOGICAL AND = %d", (n1 && n2));
        NSLog(@"LOGICAL OR = %d", (n1 || n2));
        NSLog(@"LOGICAL NOT = %d", (!n1));
    
        [pool drain];
        return 0;
    }

    Output:

    LOGICAL AND = 0
    LOGICAL OR = 1
    LOGICAL NOT = 0

    5. Bitwise Operators: Bitwise operators operate on bits and are often used for low-level programming.

    pqp & qp | qp ^ q
    00000
    01011
    11110
    10011

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        // n1 = 12(00001100), n2 = 10(00001010)
        int n1 = 12, n2 = 10;
    
        // The result is 00001000
        NSLog(@"Bitwise AND(n1&n2) = %d\n", (n1 & n2));
    
        // The result is 00001110
        NSLog(@"Bitwise OR(n1|n2) = %d\n", (n1 | n2));
    
        // The result is 00101000
        NSLog(@"LEFT SHIFT(n2<<2) = %d\n", (n2 << 2));
    
        // The result is 00000010
        NSLog(@"RIGHT SHIFT(n2>>2) = %d\n", (n2 >> 2));
    
        // The result is 00000110
        NSLog(@"XOR(n1^n2) = %d\n", (n1 ^ n2));
    
        // The result is 11110011
        NSLog(@"ONCE COMPLIMENT(~n1) = %d\n", (~n1));
    
        [pool drain];
        return 0;
    }
    • Bitwise AND:
      n1&n2=12&10n1 \& n2 = 12 \& 10n1&n2=12&10
      Binary: 00001100&00001010=0000100000001100 \& 00001010 = 0000100000001100&00001010=00001000
      Result: 8
    • Bitwise OR:
      n1∣n2=12∣10n1 | n2 = 12 | 10n1∣n2=12∣10
      Binary: 00001100∣00001010=0000111000001100 | 00001010 = 0000111000001100∣00001010=00001110
      Result: 14
    • Left Shift (<<):
      n2<<2=10<<2n2 << 2 = 10 << 2n2<<2=10<<2
      Binary: 00001010<<2=0010100000001010 << 2 = 0010100000001010<<2=00101000
      Result: 40
    • Right Shift (>>):
      n2>>2=10>>2n2 >> 2 = 10 >> 2n2>>2=10>>2
      Binary: 00001010>>2=0000001000001010 >> 2 = 0000001000001010>>2=00000010
      Result: 2
    • XOR:
      n1⊕n2=12⊕10n1 \oplus n2 = 12 \oplus 10n1⊕n2=12⊕10
      Binary: 00001100⊕00001010=0000011000001100 \oplus 00001010 = 0000011000001100⊕00001010=00000110
      Result: 6
    • One’s Complement (~):
      ∼n1=∼12\sim n1 = \sim 12∼n1=∼12
      Binary: ∼00001100=11110011\sim 00001100 = 11110011∼00001100=11110011 (in two’s complement, signed integer representation)
      Result: -13

    6. Assignment Operators: Assignment operators assign values or results of expressions to variables.

    OperatorDescription
    =Assign value
    +=Add and assign
    -=Subtract and assign
    *=Multiply and assign
    /=Divide and assign
    %=Modulus and assign
    <<=Left shift and assign
    >>=Right shift and assign
    &=Bitwise AND and assign
    ^=Bitwise XOR and assign

    Example:

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int n1 = 20;
        n1 += 10;
        NSLog(@"After +=: %d", n1);
        n1 &= 2;
        NSLog(@"After &=: %d", n1);
    
        [pool drain];
        return 0;
    }

    Output:

    After +=: 30
    After &=: 2
    Miscellaneous or Special Operators

    Special operators are unique to programming languages and are used to perform specific tasks.

    OperatorDescriptionExample
    sizeof()Returns the size of a variable in bytes.int a = 10; sizeof(a); // Returns 4 bytes
    &Returns the address of a variable.&a; // Returns the address of 'a'
    *Used to create a pointer variable.int a = 10; int *b = &a; // Pointer to 'a'
    ?:Conditional (ternary) operator, an alternative to if-else.int number1 = 5, number2 = 10, max; max = (number1 > number2) ? number1 : number2; // max = 10

    Example:

    Size of num1 (int) = 4 bytes
    Value of num1 = 8
    Value pointed by ptr = 8
    Result (a == 10) = 0
    Result (a == 15) = 1
    Operator Precedence

    Operator precedence determines the order in which operators are evaluated in an expression based on their priority. If two or more operators have the same precedence, their associativity determines the evaluation order. Associativity can be either left-to-right or right-to-left.

    For example:
    In the expression m = 5 - 2 / 2, the result is m = 4, not m = 1, because / (division) has higher precedence than - (subtraction). First, 2 / 2 is evaluated, then 5 - 1.

    Operator Precedence Table

    OperatorAssociativity
    () [] -> . ++ --Left-to-right
    + - ! ~ ++ -- (type) * & sizeof()Right-to-left
    * / %Left-to-right
    << >>Left-to-right
    == !=Left-to-right
    ^ | Left to right
    && `Left to right
    ?:Right-to-left
    = += -= *= /= %= >>= <<= &= ^= `Right to left 
    , (comma)Left-to-right
  • Variables and Data Types

    Data Types in Objective-C

    In Objective-C, each variable is associated with a data type, and each data type requires a specific amount of memory in the system. A data type defines the kind of data a variable will store and the space it occupies in memory. Data types in Objective-C are classified as follows:

    TypeDescription
    Primitive Data TypesArithmetic types, further classified into integer and floating-point data types.
    Void TypesData types with no value or operator, used when functions do not return a value.
    Derived TypesIncludes pointers, arrays, structures, unions, and function types.
    Enumerated TypesArithmetic types used to define variables restricted to certain discrete integer values.
    Data Types and Their Properties

    Below is the list of common data types in Objective-C along with their storage sizes, format specifiers, and example constants:

    TypeStorage SizeFormat SpecifierExamples
    char1 byte%c'a''\n'
    short int2 bytes%hi%hx%ho
    unsigned short int2 bytes%hu%hx%ho
    int2 or 4 bytes%i%x%o12-970xFFE0
    unsigned int2 or 4 bytes%u%x%o12u100U
    long int8 bytes%li%lx%lo12L-2001
    unsigned long int8 bytes%lu%lx%lo12UL0xffeeUL
    long long int8 bytes%lli%llx%llo0xe5e5e5e5LL
    unsigned long long int8 bytes%llu%llx%llo12ull0xffeeULL
    float4 bytes%f%e%g%a12.34f3.1e-5f
    double8 bytes%f%e%g%a12.343.1e-5
    long double10 bytes%Lf%Le%Lg12.34L3.1e-5l
    Primitive Data Types Examples

    1. Integers: The int type is used to store whole numbers, including decimal, hexadecimal, and octal values. Unsigned integers store only positive values.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int a = 9;         // Positive integer
        int b = -9;        // Negative integer
        unsigned int c = 89U;  // Unsigned integer
        long int d = 9998L;    // Long integer
    
        NSLog(@"Positive Integer: %i", a);
        NSLog(@"Negative Integer: %d", b);
        NSLog(@"Unsigned Integer: %u", c);
        NSLog(@"Long Integer: %li", d);
    
        [pool drain];
        return 0;
    }

    Output:

    Positive Integer: 9
    Negative Integer: -9
    Unsigned Integer: 89
    Long Integer: 9998

    2. Short Integers: The short int type is used for storing 2-byte integer values, which can be positive or negative.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        short int number = 67;
        NSLog(@"Short Integer Value: %hi", number);
    
        [pool drain];
        return 0;
    }

    Output:

    Short Integer Value: 67

    3. Long Integers: The long int type is used when standard int is insufficient for large values.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        long int bigInt = 9223372036854775807;
        NSLog(@"Long Integer Value: %li", bigInt);
    
        [pool drain];
        return 0;
    }

    Output:

    Long Integer Value: 9223372036854775807

    4. Character Data Type: Stores single characters, typically 1 byte.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        char a = 'a';
        NSLog(@"Character Value: %c", a);
    
        a++;
        NSLog(@"After Increment: %c", a);
    
        [pool drain];
        return 0;
    }

    Output:

    Character Value: a
    After Increment: b

    5. Floating Point Types: Floating-point types store decimal numbers.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        float a = 9.8f;
        float b = 2.5f;
    
        NSLog(@"Float A: %f", a);
        NSLog(@"Float B: %f", b);
    
        [pool drain];
        return 0;
    }

    Output:

    Float A: 9.800000
    Float B: 2.500000

    6. Double Types: Double precision floating-point values.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        double a = 12345.678;
        NSLog(@"Double Value: %lf", a);
    
        [pool drain];
        return 0;
    }

    Output:

    Double Value: 12345.678000

    7. Boolean Type: Used to store true or false values, represented as YES or NO.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        BOOL isStudent = YES;
    
        if (isStudent) {
            NSLog(@"The person is a student.");
        } else {
            NSLog(@"The person is not a student.");
        }
    
        [pool drain];
        return 0;
    }

    Output:

    The person is a student.

    8. String Data Type: Strings are objects created using the NSString class.

    Example:

    #import <Foundation/Foundation.h>
    int main() {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        NSString *name = @"Objective-C";
        NSLog(@"Programming Language: %@", name);
    
        [pool drain];
        return 0;
    }

    Output:

    Programming Language: Objective-C

    Variables in Objective-C

    A variable is a name given to a storage area that programs can manipulate. Each variable in Objective-C has a specific type that determines the size and layout of the variable’s memory, the range of values that can be stored, and the operations that can be applied.

    The name of a variable can be composed of letters, digits, and underscores. It must begin with a letter or an underscore. Uppercase and lowercase letters are distinct because Objective-C is case-sensitive. The following are the basic variable types in Objective-C:

    Sr. No.TypeDescription
    1charTypically a single octet (one byte). This is an integer type.
    2intThe most natural size of integer for the machine.
    3floatA single-precision floating-point value.
    4doubleA double-precision floating-point value.
    5voidRepresents the absence of type.

    Objective-C also allows various other types of variables, such as enumerations, pointers, arrays, structures, and unions, which are covered in later chapters. This section focuses on basic variable types.

    Variable Definition in Objective-C

    A variable definition tells the compiler where and how much storage to allocate for the variable. A definition specifies the data type and a list of one or more variables of that type as follows:

    type variable_list;

    Here, type must be a valid Objective-C data type, such as charintfloatdoubleBOOL, or any user-defined object, and variable_list may consist of one or more identifier names separated by commas. Examples include:

    int i, j, k;
    char c, ch;
    float f, salary;
    double d;

    Variables can be initialized during their declaration using the following syntax:

    type variable_list;

    Here, type must be a valid Objective-C data type, such as charintfloatdoubleBOOL, or any user-defined object, and variable_list may consist of one or more identifier names separated by commas. Examples include:

    int i, j, k;
    char c, ch;
    float f, salary;
    double d;

    Variables can be initialized during their declaration using the following syntax:

    type variable_name = value;

    Examples:

    int a = 3, b = 5;      // Definition and initialization
    char x = 'x';          // The variable x has the value 'x'
    float pi = 3.14;       // Definition and initialization

    For variables without an initializer:

    • Variables with static storage duration are initialized to NULL (all bytes have the value 0).
    • The initial value of all other variables is undefined.
    Variable Declaration in Objective-C

    A variable declaration informs the compiler of a variable’s type and name, enabling the compiler to proceed with compilation without requiring the variable’s complete details. Declarations are particularly useful when working with multiple files. For instance, you can declare a variable in one file and define it in another using the extern keyword.

    extern int a, b;
    extern float pi;

    Though a variable can be declared multiple times, it can be defined only once in a file, function, or block of code.

    Examples:

    #import <Foundation/Foundation.h>
    
    // Variable declaration
    extern int a, b;
    extern int c;
    extern float f;
    
    int main () {
      /* Variable definition */
      int a, b;
      int c;
      float f;
    
      /* Variable initialization */
      a = 10;
      b = 20;
    
      c = a + b;
      NSLog(@"Value of c: %d", c);
    
      f = 70.0 / 3.0;
      NSLog(@"Value of f: %f", f);
    
      return 0;
    }

    Output:

    Value of c: 30
    Value of f: 23.333334
    Lvalues and Rvalues in Objective-C

    Expressions in Objective-C can be categorized as:

    • Lvalue: Refers to a memory location and can appear on the left or right side of an assignment.
    • Rvalue: Refers to a data value stored at an address and can appear only on the right side of an assignment.

    Example of Valid assignment:

    int g = 20;

    Example of  Invalid assignment:

    10 = 20; // Compile-time error
  • Overview of Objective-C

    Introduction to Objective-C

    Objective-C is an object-oriented programming language that extends the C language with additional features, primarily for use in macOS and iOS applications. It was originally developed in the early 1980s by Brad Cox and Tom Love and later became the primary programming language used by Apple for its software development.

    Uses of Objective-C

    1. iOS and macOS App Development: Objective-C has been the cornerstone for developing applications on Apple platforms, ranging from mobile and desktop applications to utilities and games.
    2. Legacy Codebases: Many existing iOS and macOS projects are built using Objective-C. Developers with expertise in Objective-C are often needed to maintain, extend, or modernize these projects.
    3. Cocoa and Cocoa Touch Frameworks: These frameworks are integral to macOS and iOS development. Objective-C seamlessly integrates with these frameworks, enabling developers to create user interfaces, handle events, and access system services.
    4. Open Source Projects: Objective-C is commonly used in open-source libraries and tools. For example, GNUStep offers a cross-platform implementation of the Cocoa API for Objective-C development.
    5. High-Level Abstractions: Its object-oriented features allow developers to create modular and reusable software components using classes, objects, inheritance, and polymorphism.

     Comparison: Objective-C vs. Swift

    CriteriaObjective-CSwift
    SyntaxSyntax is verbose and uses square brackets for method calls, which can be unfamiliar.Syntax is concise and modern, utilizing dot notation for method calls, similar to other languages.
    ReadabilityRequires manual handling of pointers and memory, making code complex.Offers features like optionals and type inference, enhancing readability and reducing boilerplate.
    PerformanceReliable due to years of optimization but dynamic typing can impact performance.Designed for speed and efficiency with modern compilers, outperforming Objective-C in many cases.
    SafetyRelies on manual memory management and lacks compile-time nullability checks.Prioritizes safety with features like optionals, strong typing, and compile-time checks.
    InteroperabilityEasily integrates with Swift via bridging headers, allowing incremental migration.Can call Objective-C APIs directly, ensuring seamless interaction between the two languages.
    Learning CurveSteep due to complex syntax and manual memory management.Easier for developers familiar with modern languages, offering a beginner-friendly experience.
    CommunityLarge existing codebase but declining adoption in favor of Swift.Rapidly growing community with strong Apple support and open-source adoption beyond Apple platforms.

    Characteristics of Objective-C

    1. Object-Oriented: Objective-C is built on top of C, adding object-oriented capabilities, which allow developers to define classes and objects, promoting code reusability and modularity.
    2. Dynamic Typing: Objective-C supports dynamic typing, which means that the type of an object is determined at runtime, allowing for more flexible and adaptable code.
    3. Message Passing: Unlike function calls in C, Objective-C uses a messaging model similar to Smalltalk. Messages are sent to objects, and the method invoked is determined at runtime, enabling polymorphism and dynamic behavior.
    4. Categories and Protocols: Categories allow you to add methods to existing classes without modifying the original class. Protocols define a set of methods that a class can implement, similar to interfaces in other languages, facilitating polymorphic behavior.
    5. Interoperability with C and C++: Objective-C is fully compatible with C, allowing developers to include C code and libraries directly. It can also work with C++ code using Objective-C++.
    6. Automatic Reference Counting (ARC): ARC is a memory management feature that automatically handles the retain and release of objects, reducing the need for manual memory management and minimizing memory leaks.
    7. Rich Foundation Framework: Objective-C includes a powerful Foundation framework that provides essential classes and APIs for data management, collections, string manipulation, and more, forming the core of macOS and iOS development.
    8. Cocoa and Cocoa Touch Frameworks: These frameworks provide a comprehensive set of APIs for building macOS and iOS applications, including user interface components, networking, graphics, and data persistence.
    9. Backward Compatibility: Objective-C maintains backward compatibility with older versions of the language, allowing developers to continue using legacy code while taking advantage of new features.
    10. Bridging with Swift: Objective-C is interoperable with Swift, Apple’s modern programming language, allowing developers to gradually integrate Swift into existing Objective-C codebases.

    Limitations of Objective-C

    1. Verbose Syntax: Objective-C’s syntax, with features like square brackets for method calls, can be challenging for developers accustomed to modern languages.
    2. Declining Trend: With Swift’s emergence, the industry is shifting towards newer, more efficient languages, impacting the demand for Objective-C.
    3. Manual Memory Management: Developers must handle memory manually, increasing the risk of errors like memory leaks and crashes.
    4. Limited Safety Features: Objective-C lacks many safety features found in Swift, such as compile-time nullability checks, leading to potential runtime errors.
    5. Steep Learning Curve: New developers may find Objective-C difficult to learn due to its syntax and manual memory management requirements.
    6. Transition to Swift: While Objective-C and Swift are interoperable, migrating projects or integrating codebases can be complex and time-consuming.

    Examples and Outputs

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // Basic program to print a message
            NSLog(@"Hello from Objective-C!");
        }
        return 0;
    }

    Output:

    Hello from Objective-C!
  • Objective-C Tutorial Roadmap

    Introduction to Objective-C

    What is Objective-C?

    Objective-C is an object-oriented programming language that extends the C language with messaging and runtime features, widely used for macOS and iOS development before Swift.

    Characteristics of Objective-C

    • Object-oriented and dynamic
    • Superset of the C programming language
    • Message-passing syntax
    • Powerful runtime system
    • Seamless integration with C and C++

    Variables and Data Types in Objective-C

    Data Types in Objective-C

    • Primitive data types
    • Derived data types
    • User-defined data types

    Variables

    • Variable declaration and initialization
    • Scope and lifetime of variables

    Operators in Objective-C

    Types of Operators

    • Arithmetic operators
    • Relational operators
    • Logical operators
    • Assignment operators
    • Bitwise operators

    Loops in Objective-C

    Types of Loops

    • for loop
    • while loop
    • do-while loop
    • Nested loops

    Functions in Objective-C

    Defining and Calling Functions

    • Function syntax
    • Function declarations and definitions

    Parameters and Return Values

    • Passing arguments to functions
    • Returning values

    Function Pointers

    • Understanding function pointers
    • Use cases and examples

    Blocks in Objective-C

    Introduction to Blocks

    • What are blocks?
    • Syntax of blocks

    Types of Blocks

    • Global blocks
    • Stack blocks
    • Heap blocks

    Numbers in Objective-C

    Number Types

    • Integer types
    • Floating-point numbers
    • NSNumber class

    Arrays in Objective-C

    Working with Arrays

    • Creating arrays
    • Accessing and modifying array elements

    Object-Oriented Programming in Objective-C

    Classes and Objects

    • Syntax and keywords
    • Types of classes
    • Types of objects

    Creating Subclasses

    • Inheritance basics

    Method Overriding

    • Overriding parent class methods
    • Using super to access parent methods

    Dynamic Typing and Polymorphism

    • Dynamic method resolution
    • Runtime polymorphism

    Pointers in Objective-C

    Pointer Basics

    • Understanding pointers
    • NULL pointers

    Pointer Operations

    • Pointer arithmetic
    • Array of pointers
    • Pointer to pointer

    Pointers and Functions

    • Passing pointers to functions
    • Returning pointers from functions

    Strings in Objective-C

    String Types

    • C-style strings
    • NSString and NSMutableString

    Structures in Objective-C

    Structures

    • Defining and using structures

    Pointers to Structures

    • Accessing structure members using pointers

    Preprocessors in Objective-C

    Preprocessors

    • Macro definitions
    • File inclusion

    Types of Preprocessors

    • #define
    • #include
    • Conditional compilation

    Typedef in Objective-C

    Typedef Usage

    • Creating aliases using typedef

    Typedef vs #define

    • Key differences and use cases

    Type Casting in Objective-C

    Type Casting

    • Implicit type casting
    • Explicit type casting

    Error, Log, and File Handling in Objective-C

    Log Handling

    • Using NSLog
    • Debugging techniques

    File Handling

    • Reading from files
    • Writing to files

    Error Handling

    • NSError and exception handling

    Command Line Arguments in Objective-C

    Command Line Arguments

    • Accessing arguments in Objective-C programs
    • Practical examples

    Inheritance in Objective-C

    Types of Inheritance

    • Single inheritance
    • Multilevel inheritance

    Data Encapsulation in Objective-C

    Encapsulation Concepts

    • Access specifiers
    • Data hiding and abstraction

    Categories in Objective-C

    Introduction to Categories

    • Extending existing classes

    Category Usage

    • Adding methods without subclassing

    Posing in Objective-C

    Posing

    • Understanding posing
    • Types of posing

    Extensions in Objective-C

    Class Extensions

    • Private methods and properties
    • Difference between categories and extensions

    Dynamic Binding in Objective-C

    Dynamic Binding

    • Runtime method binding

    Use Cases

    • Flexibility and dynamic behavior

    Composite Objects in Objective-C

    Composite Objects

    • Object composition

    Types of Composite Objects

    • Aggregation
    • Composition

    Foundation Framework in Objective-C

    Introduction to Foundation Framework

    • Core classes and utilities

    Types and Subtypes

    • Collections
    • Strings
    • Dates
    • File management

    Fast Enumeration in Objective-C

    Fast Enumeration

    • Iterating collections efficiently

    Memory Management in Objective-C

    Memory Management Concepts

    • Reference counting
    • ARC (Automatic Reference Counting)

    Best Practices

    • Avoiding memory leaks
    • Managing object lifecycles

  • DOM Function

    DOMAttr

    __construct()
    Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
    Example:

    $doc = new DOMDocument();
    $attr = new DOMAttr("id", "unique-value");
    echo $attr->name;  // Output: id
    echo $attr->value; // Output: unique-value

    isId()
    Description: Checks if this attribute is an ID attribute.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example");
    $attr = $doc->createAttribute("id");
    $attr->value = "unique-id";
    $element->setAttributeNode($attr);
    echo $attr->isId() ? "true" : "false";
    // Output: true (if the attribute is declared as an ID in a DTD)

    DOMCdataSection

    __construct()
    Description: Constructs a new DOMCdataSection object. This is used to create a CDATA section within the document.
    Example:

    $doc = new DOMDocument();
    $cdata = $doc->createCDATASection("This is a CDATA section.");
    $doc->appendChild($cdata);
    echo $doc->saveXML();
    // Output: <!DOCTYPE example><![CDATA[This is a CDATA section.]]&gt;

    DOMDocument

    __construct()
    Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
    Example:

    $doc = new DOMDocument();
    $attr = new DOMAttr("id", "unique-value");
    echo $attr->name;  // Output: id
    echo $attr->value; // Output: unique-value

    createAttribute()
    Description: Creates a new attribute node with the specified name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example");
    $attr = $doc->createAttribute("id");
    $attr->value = "unique-id";
    $element->setAttributeNode($attr);
    echo $attr->isId() ? "true" : "false";
    // Output: true (if the attribute is declared as an ID in a DTD)

    count()
    Description: Get the count of elements present in a PriorityQueue instance.
    Example:

    $doc = new DOMDocument();
    $attr = $doc->createAttribute("class");
    $attr->value = "my-class";
    echo $attr->name;  // Output: class

    createAttributeNS()
    Description: Creates a new attribute with the specified namespace URI and qualified name.
    Example:

    $doc = new DOMDocument();
    $attr = $doc->createAttributeNS("http://example.com", "ex:attr");
    $attr->value = "value";
    echo $attr->name;  // Output: ex:attr

    createCDATASection()
    Description: Creates a new CDATA section.
    Example:

    $doc = new DOMDocument();
    $cdata = $doc->createCDATASection("CDATA Content");
    $doc->appendChild($cdata);
    echo $doc->saveXML();
    // Output: <!DOCTYPE example><![CDATA[CDATA Content]]&gt;

    createComment()
    Description: Creates a new comment node.
    Example:

    $doc = new DOMDocument();
    $comment = $doc->createComment("This is a comment.");
    $doc->appendChild($comment);
    echo $doc->saveXML();
    // Output: <!--This is a comment.-->

    createDocumentFragment()
    Description: Creates an empty DOMDocumentFragment object.
    Example:

    $doc = new DOMDocument();
    $fragment = $doc->createDocumentFragment();
    $fragment->appendXML("<example>Content</example>");
    $doc->appendChild($fragment);
    echo $doc->saveXML();
    // Output: <example>Content</example>

    createElement()
    Description: Creates a new element with the given tag name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example", "This is content.");
    $doc->appendChild($element);
    echo $doc->saveXML();
    // Output: <example>This is content.</example>

    createElementNS()
    Description: Creates a new element with a namespace URI and qualified name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElementNS("http://example.com", "ex:example", "Content");
    $doc->appendChild($element);
    echo $doc->saveXML();
    // Output: <ex:example xmlns:ex="http://example.com">Content</ex:example>

    createEntityReference()
    Description: Creates a new entity reference.
    Example:

    $doc = new DOMDocument();
    $entity = $doc->createEntityReference("copy");
    $doc->appendChild($entity);
    echo $doc->saveXML();
    // Output: &copy;

    createProcessingInstruction()
    Description: Creates a new processing instruction.
    Example:

    $doc = new DOMDocument();
    $pi = $doc->createProcessingInstruction("php", "echo 'Hello';");
    $doc->appendChild($pi);
    echo $doc->saveXML();
    // Output: <?php echo 'Hello';?>

    createTextNode()
    Description: Creates a new text node.
    Example:

    $doc = new DOMDocument();
    $text = $doc->createTextNode("This is text.");
    $doc->appendChild($text);
    echo $doc->saveXML();
    // Output: This is text.

    getElementById()
    Description: Returns an element by its ID.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML('<example id="unique">Content</example>');
    echo $doc->getElementById("unique")->nodeValue;
    // Output: Content

    getElementsByTagName()
    Description: Returns a list of elements by tag name.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML("<root><child>One</child><child>Two</child></root>");
    $elements = $doc->getElementsByTagName("child");
    foreach ($elements as $element) {
        echo $element->nodeValue . "\n";
    }
    // Output: One Two

    getElementsByTagNameNS()
    Description: Returns a list of elements by tag name within a namespace.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML('<root xmlns:ex="http://example.com"><ex:child>Content</ex:child></root>');
    $elements = $doc->getElementsByTagNameNS("http://example.com", "child");
    foreach ($elements as $element) {
        echo $element->nodeValue . "\n";
    }
    // Output: Content

    importNode()
    Description: Imports a node from another document.
    Example: 

    $doc1 = new DOMDocument();
    $doc1->loadXML("<example>Content</example>");
    $doc2 = new DOMDocument();
    $imported = $doc2->importNode($doc1->documentElement, true);
    $doc2->appendChild($imported);
    echo $doc2->saveXML();
    // Output: <example>Content</example>

    load()
    Description: Loads an XML document from a file.
    Example:

    $doc = new DOMDocument();
    $doc->load("file.xml");
    echo $doc->saveXML();

    DOMDocumentFragment

    DOMDocumentFragment appendXML() Function
    Description: Find and return an element in the PriorityQueue by a condition.
    Example:

    $doc = new DOMDocument();
    $fragment = $doc->createDocumentFragment();
    
    $xml = "<item>First Item</item><item>Second Item</item>";
    $fragment->appendXML($xml);
    
    $root = $doc->createElement("root");
    $doc->appendChild($root);
    $root->appendChild($fragment);
    
    echo $doc->saveXML();
    // Output:
    // <root><item>First Item</item><item>Second Item</item></root>

    __construct()
    Description: Initializes a new DOMElement object.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Hello World");
    echo $element->nodeName;
    // Output: example

    DOMElement

    __construct()

    Description: Initializes a new DOMElement object.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $reversed = array_reverse($pq->toArray());
    
    print_r($reversed);
    // Output: Array ( [0] => One [1] => Two [2] => Three )

    getAttribute()
    Description: Retrieves the value of an attribute by name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttribute('id', '123');
    echo $element->getAttribute('id');
    // Output: 123

    getAttributeNode()
    Description: Retrieves the attribute node by name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttribute('id', '123');
    $attrNode = $element->getAttributeNode('id');
    echo $attrNode->name . ' = ' . $attrNode->value;
    // Output: id = 123

    getAttributeNodeNS()
    Description: Retrieves an attribute node by its namespace URI and name.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Another One", 1);
    
    $priorityToExtract = 1;
    $extracted = array_filter($pq->toArray(), function ($item) use ($priorityToExtract) {
        return $item[1] === $priorityToExtract;
    });
    
    print_r($extracted);
    // Output: Array ( [0] => One [1] => Another One )

    getAttributeNS()
    Description: Retrieves the value of an attribute by namespace URI and name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttributeNS('http://example.com/ns', 'ex:id', '789');
    echo $element->getAttributeNS('http://example.com/ns', 'id');
    // Output: 789

    DOMEntityReference

    __construct()
    Description: Initializes a new DOMEntityReference object, which represents an entity reference in the DOM. Entity references are placeholders for entities that are replaced by their defined content in the XML or HTML document.

    Example:

    $doc = new DOMDocument();
    $entityRef = $doc->createEntityReference('copy');
    $doc->appendChild($entityRef);
    echo $doc->saveXML();
    // Output: &copy;

    DOMImplementation

    __construct()

    Description: Initializes a new DOMImplementation object, which provides methods for operations independent of the document instance.

    Example:

    $domImpl = new DOMImplementation();
    var_dump($domImpl instanceof DOMImplementation);
    // Output: bool(true)

    createDocument()
    Description: Creates a new DOMDocument object with an optional namespace URI, qualified name, and document type.
    Example:

    $domImpl = new DOMImplementation();
    $doc = $domImpl->createDocument("http://www.example.com", "example:root");
    echo $doc->saveXML();
    // Output: <?xml version="1.0"?><example:root xmlns:example="http://www.example.com"/>

    createDocumentType()
    Description: Creates a new DOMDocumentType object. It is used to define the document type declaration.
    Example:

    $domImpl = new DOMImplementation();
    $doctype = $domImpl->createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
    echo $doctype->name; // Output: html

    hasFeature()
    Description: Checks if the DOMImplementation object supports a specific feature and version.
    Example:

    $doc = new DOMDocument();
    $attr = $doc->createAttribute('id');
    $attr->value = '456';
    $element = $doc->createElement('example');
    $element->setAttributeNode($attr);
    echo $element->getAttribute('id');
    // Output: 456

    setAttributeNodeNS()
    Description: Adds a new attribute node with a namespace to the element.
    Example:

    $domImpl = new DOMImplementation();
    if ($domImpl->hasFeature("XML", "1.0")) {
        echo "Feature XML 1.0 is supported.";
    } else {
        echo "Feature not supported.";
    }
    // Output: Feature XML 1.0 is supported.

    DOMNamedNodeMap

    count()
    Description: Returns the number of items in the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2" attr3="value3"/>');
    $attributes = $dom->documentElement->attributes;
    echo $attributes->count();
    // Output: 3

    getNamedItem()
    Description: Retrieves a node specified by name from the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2"/>');
    $attributes = $dom->documentElement->attributes;
    $attr = $attributes->getNamedItem('attr1');
    echo $attr->nodeValue;
    // Output: value1

    item()
    Description: Returns a node at the specified index in the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2"/>');
    $attributes = $dom->documentElement->attributes;
    $attr = $attributes->item(0);
    echo $attr->nodeName . ": " . $attr->nodeValue;
    // Output: attr1: value1

    DOMNode

    appendChild()
    Description: Appends a new child node to the DOM node. This function adds the node to the list of children of the current node.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttributeNS('http://example.com/ns', 'ex:id', 'unique');
    $element->setIdAttributeNS('http://example.com/ns', 'id', true);
    echo $doc->getElementById('unique')->nodeName;
    // Output: example

    C14N()
    Description: Canonicalizes the XML of the current node and returns it as a string.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Hello World!");
    $dom->appendChild($element);
    $element->C14NFile("example.xml");
    // The file "example.xml" will contain the canonicalized XML.

    cloneNode()
    Description: Creates a duplicate of the current node. It can clone the node with or without its children.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Clone me!");
    $dom->appendChild($element);
    $clone = $element->cloneNode(true);
    $dom->appendChild($clone);
    echo $dom->saveXML();
    // Output: <example>Clone me!</example><example>Clone me!</example>

    getLineNo()
    Description: Returns the line number of the current node within the document.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><child>Content</child></root>');
    $child = $dom->getElementsByTagName("child")->item(0);
    echo $child->getLineNo();
    // Output: 2 (This depends on the line where the <child> element is located)

    getNodePath()
    Description: Returns the XPath of the node.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><parent><child>Value</child></parent></root>');
    $child = $dom->getElementsByTagName("child")->item(0);
    echo $child->getNodePath();
    // Output: /root/parent/child

    hasAttributes()
    Description: Checks if the current node has attributes.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    $element->setAttribute("attr", "value");
    echo $element->hasAttributes() ? "Has attributes" : "No attributes";
    // Output: Has attributes

    hasChildNodes()
    Description: Checks if the current node has child nodes.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    echo $parent->hasChildNodes() ? "Has child nodes" : "No child nodes";
    // Output: Has child nodes

    insertBefore()
    Description: Inserts a new node before a specified reference node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child1 = $dom->createElement("child1");
    $child2 = $dom->createElement("child2");
    $parent->appendChild($child1);
    $parent->insertBefore($child2, $child1);
    echo $dom->saveXML();
    // Output: <parent><child2/><child1/></parent>

    isDefaultNamespace()
    Description: Checks if the given namespace URI is the default namespace for the current node.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->isDefaultNamespace("http://www.example.com") ? "Default namespace" : "Not default namespace";
    // Output: Default namespace

    isSameNode()
    Description: Checks if two nodes are the same node.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    $clone = $element->cloneNode();
    echo $element->isSameNode($clone) ? "Same node" : "Different nodes";
    // Output: Different nodes

    isSupported()
    Description: Checks if the current node supports the specified feature and version.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    echo $element->isSupported("core", "1.0") ? "Supported" : "Not supported";
    // Output: Supported

    lookupNamespaceUri()
    Description: Looks up the URI associated with a namespace prefix.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->lookupNamespaceUri("example");
    // Output: http://www.example.com

    lookupPrefix()
    Description: Looks up the prefix associated with a given namespace URI.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->lookupPrefix("http://www.example.com");
    // Output: example

    normalize()
    Description: Normalizes the current node by merging adjacent text nodes and removing empty text nodes.
    Example:

    $dom = new DOMDocument();
    $textNode = $dom->createTextNode("Hello");
    $space = $dom->createTextNode(" ");
    $otherTextNode = $dom->createTextNode("World!");
    $element = $dom->createElement("example");
    $element->appendChild($textNode);
    $element->appendChild($space);
    $element->appendChild($otherTextNode);
    $element->normalize();
    echo $dom->saveXML();
    // Output: <example>Hello World!</example>

    removeChild()
    Description: Removes a child node from the current node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    $parent->removeChild($child);
    echo $dom->saveXML();
    // Output: <parent/>

    removeChild()
    Description: Removes a child node from the current node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    $parent->removeChild($child);
    echo $dom->saveXML();
    // Output: <parent/>

    replaceChild()
    Description: Replaces a child node with another node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child1 = $dom->createElement("child1");
    $child2 = $dom->createElement("child2");
    $parent->appendChild($child1);
    $parent->replaceChild($child2, $child1);
    echo $dom->saveXML();
    // Output: <parent><child2/></parent>

    DOMProcessingInstruction

    DOMProcessingInstruction __construct()
    Description: Constructs a new processing instruction. This is typically used to add instructions for applications like XML stylesheets.
    Example:

    $dom = new DOMDocument();
    $pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
    $dom->appendChild($pi);
    echo $dom->saveXML();
    // Output: <?xml-stylesheet type="text/xsl" href="style.xsl"?>

    DOMProcessingInstruction data()
    Description: Retrieves or sets the content of the processing instruction.
    Example:

    $dom = new DOMDocument();
    $pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
    $dom->appendChild($pi);
    echo $pi->data;
    // Output: type="text/xsl" href="style.xsl"

    DOMProcessingInstruction target()
    Description: Retrieves the target of the processing instruction, such as the application it is intended for.
    Example:

    DOMText

    PHP DOMText __construct() Function
    Description: Constructs a new DOMText object. A DOMText node is used to represent text within an element or attribute in the DOM.
    Example:

    $dom = new DOMDocument();
    $textNode = new DOMText("Hello, World!");
    $dom->appendChild($textNode);
    echo $dom->saveXML();
    // Output: Hello, World!

    PHP DOMText isElementContentWhitespace() Function
    Description: Checks if the text node contains only whitespace and is part of the element’s content.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root>   <child>Content</child>   </root>");
    $textNode = $dom->documentElement->firstChild;
    echo $textNode->isElementContentWhitespace() ? "Yes" : "No";
    // Output: Yes

    PHP DOMText isWhitespaceInElementContent() Function
    Description: An alias for isElementContentWhitespace(). Checks if the text node contains only whitespace and is part of the element’s content.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root>   <child>Content</child>   </root>");
    $textNode = $dom->documentElement->firstChild;
    echo $textNode->isWhitespaceInElementContent() ? "Yes" : "No";
    // Output: Yes

    PHP DOMText splitText() Function
    Description: Splits a text node into two at the specified offset. The original node contains the text before the offset, and a new node is created with the text after the offset.
    Example:

    $dom = new DOMDocument();
    $textNode = $dom->createTextNode("Hello, World!");
    $dom->appendChild($textNode);
    
    $newNode = $textNode->splitText(7);
    echo $textNode->wholeText . "\n"; // Output: Hello,
    echo $newNode->wholeText;        // Output: World!

    DOMXPath

    PHP DOMXPath __construct() Function
    Description: Creates a new DOMXPath object for querying and evaluating XPath expressions on a DOMDocument.

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value</item></root>");
    $xpath = new DOMXPath($dom);
    echo "DOMXPath object created.";

    PHP DOMXPath evaluate() Function
    Description: Evaluates an XPath expression and returns the result. The result can be a DOMNodeList, a string, a number, or a boolean, depending on the expression.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value</item></root>");
    $xpath = new DOMXPath($dom);
    $result = $xpath->evaluate("string(/root/item)");
    echo $result; // Output: Value

    PHP DOMXPath query() Function
    Description: Executes an XPath query and returns a DOMNodeList containing all nodes matching the expression.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value1</item><item>Value2</item></root>");
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query("//item");
    
    foreach ($nodes as $node) {
        echo $node->nodeValue . "\n";
    }
    // Output:
    // Value1
    // Value2

    PHP DOMXPath registerNamespace() Function
    Description: Registers a namespace with a prefix for use in XPath queries.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root xmlns:ns="http://example.com"><ns:item>Value</ns:item></root>');
    $xpath = new DOMXPath($dom);
    $xpath->registerNamespace("ns", "http://example.com");
    $result = $xpath->evaluate("string(//ns:item)");
    echo $result; // Output: Value

    PHP DOMXPath registerPhpFunctions() Function
    Description: Enables PHP functions to be used within XPath expressions. By default, PHP functions are not available in XPath queries.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><item>Value</item></root>');
    $xpath = new DOMXPath($dom);
    $xpath->registerPhpFunctions();
    $result = $xpath->evaluate("php:function('strtoupper', /root/item)");
    echo $result; // Output: VALUE

    DOM Functions

    PHP dom_import_simplexml() Function
    Description:The dom_import_simplexml() function converts a SimpleXMLElement object into a DOMElement object, allowing you to work with the node using the DOM extension methods.
    Example:

    // Create a SimpleXMLElement object
    $simpleXml = simplexml_load_string("<root><item>Value</item></root>");
    
    // Import the SimpleXMLElement into a DOMElement
    $domElement = dom_import_simplexml($simpleXml);
    
    if ($domElement) {
        echo $domElement->nodeName; // Output: root
    }
  • Ds\Vector Functions

    Ds\Vector Functions

    allocate()
    Description: Provides the custom size of the vector to allocate space.
    Example:

    $vector = new Vector();
    $vector->allocate(10);
    echo $vector->capacity();
    // Output: 10

    apply()
    Description: Updates all values in the array by applying the callback function to each value of the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->apply(fn($value) => $value * 2);
    print_r($vector->toArray());
    // Output: [2, 4, 6]

    capacity()
    Description: Returns the current capacity of the vector.
    Example:

    $vector = new Vector();
    echo $vector->capacity();
    // Output: 0

    clear()
    Description: Clears the vector elements by removing all the elements.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->clear();
    echo $vector->isEmpty();
    // Output: 1 (true)

    __construct()
    Description: Creates a new instance.
    Example:

    $vector = new Vector([1, 2, 3]);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

    contains()
    Description: Checks whether the vector contains the given value.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->contains(2);
    // Output: 1 (true)

    copy()
    Description: Returns a shallow copy of the set.
    Example:

    $result = gmp_div_qr("10", "3");
    // Output: ([quotient => 3, remainder => 1])

    copy()
    Description: Creates a copy of the given vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    $copy = $vector->copy();
    print_r($copy->toArray());
    // Output: [1, 2, 3]

    count()
    Description: Counts the number of elements in the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->count();
    // Output: 3

    filter()
    Description: Filters out elements that satisfy the condition defined in the callback function.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $result = $vector->filter(fn($value) => $value > 2);
    print_r($result->toArray());
    // Output: [3, 4]

    find()
    Description: Finds the index of the element in the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->find(2);
    // Output: 1

    first()
    Description: Returns the first element in the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->first();
    // Output: 1

    get()
    Description: Returns the element at the given index.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->get(1);
    // Output: 2

    insert()
    Description: Inserts the element into the vector at the given index.
    Example:

    $vector = new Vector([1, 3]);
    $vector->insert(1, 2);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

    isEmpty()
    Description: Checks whether the vector is empty.
    Example:

    $vector = new Vector();
    echo $vector->isEmpty();
    // Output: 1 (true)

    join()
    Description: Joins elements of the vector into a string using the specified separator.
    Example:

    $vector = new Vector(["a", "b", "c"]);
    echo $vector->join(",");
    // Output: a,b,c

    jsonSerialize()
    Description: Returns elements that can be converted to JSON.
    Example:

    $vector = new Vector(["a", "b", "c"]);
    echo json_encode($vector);
    // Output: ["a","b","c"]

    last()
    Description: Returns the last element of the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->last();
    // Output: 3

    map()
    Description: Returns the result of a callback after applying it to each value in the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    $result = $vector->map(fn($value) => $value * 2);
    print_r($result->toArray());
    // Output: [2, 4, 6]

    merge()
    Description: Merges all the elements into the vector.
    Example:

    $vector = new Vector([1, 2]);
    $vector->merge([3, 4]);
    print_r($vector->toArray());
    // Output: [1, 2, 3, 4]

    pop()
    Description: Removes the last element of the vector and returns it.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->pop();
    // Output: 3

    push()
    Description: Adds elements to the end of the vector.
    Example:

    $vector = new Vector([1, 2]);
    $vector->push(3);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

    reduce()
    Description: Reduces the vector to a single value by applying operations in the callback function.
    Example:

    $vector = new Vector([1, 2, 3]);
    $result = $vector->reduce(fn($carry, $value) => $carry + $value, 0);
    echo $result;
    // Output: 6

    remove()
    Description: Removes and returns a value by index.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->remove(1);
    // Output: 2

    reverse()
    Description: Reverses the vector elements in place.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->reverse();
    print_r($vector->toArray());
    // Output: [3, 2, 1]

    rotate()
    Description: Rotates the array elements by a given number of rotations. Rotations happen in place.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $vector->rotate(2);
    print_r($vector->toArray());
    // Output: [3, 4, 1, 2]

    set()
    Description: Sets the value in the vector at the given index.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->set(1, 5);
    print_r($vector->toArray());
    // Output: [1, 5, 3]

    shift()
    Description: Removes the first element from the vector and returns it.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->shift();
    // Output: 1

    slice()
    Description: Returns the sub-vector of the given vector.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $subVector = $vector->slice(1, 3);
    print_r($subVector->toArray());
    // Output: [2, 3]

    sort()
    Description: Sorts the elements of the vector in place.
    Example:

    $vector = new Vector([3, 1, 2]);
    $vector->sort();
    print_r($vector->toArray());
    // Output: [1, 2, 3]