Objective C & Swift Programming Switch Statements
https://unsplash.com/photos/EJMTKCZ00I0

Objective C & Swift Programming Switch Statements


Today, we will discuss two languages commonly used in IOS development: Swift programming language and Objective C. We will target two objectives with this article, including syntax fundamentals and familiarity between both languages.

Objective C programming

Referring to the example below, we can see that our switch statement will be looking for the value "15" in stormCategory. The switch statement executes as a selection mechanism that utilizes control flow to find the equal value of a variable inside specific case statements. In this example, there are no cases that hold the number 15, so the Objective C compiler will execute the default case listed below.

int main(int argc, const char * argv[]) 
    @autoreleasepool {
        int stormCategory = 15;
        
        switch (stormCategory) {
            case 1:
                NSLog(@"HelloWorld");
                break;
            case 2:
                NSLog(@"NoHelloWorld");
                break;
            case 3:
                
            case 5:
                NSLog(@"Kind Hwlloworld");
                break;
            default:
                NSLog(@"Very well HelloWorld");
        }
    }
    return 0;
}

Now let's say that we change the initial value of "storm Category" to number 5. What do you think will happen in our small program? The short answer is Objective C compiler will locate their case with the number 5. So if there is a case that holds number 5, it will execute that case and end the program. Refer to the example below for clarification.

int main(int argc, const char * argv[]) 
    @autoreleasepool {
        int stormCategory = 5;
        // We give the storm category variable a value of 5
        
        switch (stormCategory) {
       //case 1 executes
            case 1:
                NSLog(@"HelloWorld");
                break;
            case 2:
                NSLog(@"NoHelloWorld");
                break;
            case 3:
                
            case 5:
                NSLog(@"Kind Helloworld");
                break;
            default:
                NSLog(@"Very well HelloWorld");
        }
    }
    return 0;
}

Swift Programming

Switch statements in Objective C are similar to Swift programming syntax, and both follow the same rules and understanding that we are accustomed to. Listed below, we have a Swift programming Switch statement that switches the "initial" let value. The swift compiler will first see whether the first case statement contains an "H" string value and precisely execute that case in the program. Our console will now run "this is H."

let initial = "H"
let hp = 26
let mp = 24
switch initial {
case "H":
print("this is H")
case "A":
print("This is A")
default:
print("not the letter")
}

Let us look at another example using Swift programming language.

Looking at this example, we see that instead of switching through the "H" let value, we are now going through "hp," looking for an equal integer value of 26. The cases we now change into integer values as Swift compiler will not allow us to have integer value as our switch value and cases to be string values

let initial = "H"
let hp = 26
let mp = 24
switch hp {
    //The switch statement will now cycle through find equal value for 26.
case 3:
print("this is H")
case 26:
print("This is A")
default:
print("not the letter")
}

Conclusion

Today, we covered the short objective of understanding switch statements in IOS development for Objective C and Swift programming language. There are much more complex switch statements that we could cover, but that beats the purpose of understanding the basics and learning to walk before we run. I hope everyone got something out of this article, and happy coding! Listed below is my study material to review both languages' syntax; this truly helps me when I need to memorize specific criteria, and flashcards can challenge your logical thinking.

要查看或添加评论,请登录

Michael Balsa的更多文章

社区洞察

其他会员也浏览了