Swift Language Basics Tutorial - Simple Values(let, var) and Print output on screen
Model - Nurujjaman Pollob |
Welcome to Swift's basic training tutorial. If you are a new who want to learn Swift to develop Apple platform-based software, then it's for you.
I will simply share the tutorial only. I am not going to share the story about what you gonna do with it, what Swift actually does, and what to build with it.
Anyway, If you need to know in detail about what to do with Swift Language, please follow this link to study on your own: https://developer.apple.com/swift/
Also, this link also helps: https://www.quora.com/What-is-Swift-programming-What-can-it-do-How-is-it-being-used-in-the-industry-Who-should-learn-this-language-and-why
Okay, here is some information about the Swift programming language:
Swift is a general-purpose programming language that combines the best in modern language thinking with wisdom from the wider Apple engineering culture and the diverse contributions from its open-source community. The compiler is optimized for performance and the language is optimized for development, without compromising on either.
This language looks familiar with Objective-C, and this Language is intended to replace Objective-C and Swift combines powerful type inference and pattern matching with a modern, lightweight syntax, allowing complex ideas to be expressed in a clear and concise manner. As a result, code is not just easier to write, but easier to read and maintain as well.
Swift defines away large classes of common programming errors by adopting modern programming patterns:
- Variables are always initialized before use.
- Array indices are checked for out-of-bounds errors.
- Integers are checked for overflow.
- Optionals ensure that nil values are handled explicitly.
- Memory is managed automatically.
- Error handling allows controlled recovery from unexpected failures.
Okay, let's dive into the setup environment and install all necessary tools to start to practice Swift Language from your favorite Device.
A macOS device is required to learn Swift Language. You need to install the Swift Playground application from the Apple App store from your iMac, iPad, or Macbook to practice Swift Language.
For iPad devices navigate to this link to download Swift Playground: https://apps.apple.com/app/id908519492
For Mac devices, go here: https://apps.apple.com/app/id1496833156
Install this software on your device. If you prefer a video tutorial of how you can download and install Swift playground, here is the tutorial:
If you don't have an iPad or Mac device, do not worry, simply go to this link and start practicing: https://swiftfiddle.com/
Anyway, I recommend your Mac device, if you have one. If you follow this video properly, you will end up with a sample project and Swift Playground installed.
The syntax I am going to share is all Swift 5.5, so it is recommended that you have set Swift version 5.5 in your code editor.
Okay, let's dig into coding...
Swift theory - Simple Values
Showing output in the screen:
print(swiftVariable);print(swiftConstant);
So, your final code will look like this:
If you are unable to see the output on the screen, you might need to watch this small video to understand how to see the output, here is the video tutorial:
Update the value of Variable:
So, let's change its value to 100, to do this write the following code after swiftVariable:
swiftVariable = 100;
So, your code should look like this:
I have got a test for you, let's put swiftVariable = 100; at the bottom of your code. what output do you get?
Did you see any difference? why did this happen, let's figure it out and write it in the comment section.
Also, try to change swiftConstant value. what output/error do you get? let's write it in the comment section.
Defining multiple constants and variables in a single line
var x = 0, y = 1, z = 3;let a = 100, b = 200, c = 300;
Comments
Post a Comment
Wow nice post!