Swift Language Basics Tutorial - Simple Values(let, var) and Print output on screen

 

A young boy with a macbook and neon background
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


Let's go to your empty playground, and clean all content, so the editor interface will look like this:



Let's write var swiftVariable = 200; in your editor interface so your editor interface will look like this:


What is var in Swift? var refers to a simple value, which has no specific type and its value can be changed. For example, the share market price usually drops and increases.

If you would like to learn more about var, here is the official documentation page from apple: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html


Let's write let swiftConstant = 200; in your editor interface, so the editor interface will look like this: 




What is let in Swift? let refers to an immutable object that can be initiated at compiler-time, and its value can't be changed. If you try to change its value, you will face a compile-time error. For example, let's say we are human, and that cant be changed.

If you would like to learn more about let, here is the official documentation page from apple: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

Showing output in the screen:


Use print statement to show output on the screen. let's add the following code in your editor interface, and then click the Run My Code button or Run button from your favorite code editor to see the output of your code.

The code is here: 


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:


We already have var swiftVariable = 200; Right?

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


Let's clean all your existing code and then write this code in the code editor:

var x = 0, y = 1, z = 3;
let a = 100, b = 200, c = 300;
So, as you know, we can't change the value of a let. to change the value of var, use the similar approach we have used to change the value of a variable named swiftVariable.

Okay, let's we gonna do some experiments, paste the following code in your code editor:

var x = 0, y = 1, z = 3;

let a = 100, b = 200, c = 300;


x = y;

y = x;

z = x;


print(x);

print(y);

print(z);

print(a);

print(b);

print(c);


So, your final code will look like this:



Let's run this code and see the output. what output did you get? can you figure out, why did this happen?

Please let me know in the comment section. This post going to be big so I am stopping here today. See you in another day, In another Day, I will going to discuss Type Annotation in the Swift language, and discuss primitive Data types.

Play with your code a bit and discover, enjoy ;)

Comments

Popular posts from this blog

MIUI 12.6 Android 11 port for Poco M3 download - Well tested ROM

How Long to Keep Business Insurance Policies | Retain and Destroy Insurance Policies

RealMe GT NEO 2 Bootloader unlock method on windows 11 - Updated