An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
Learn with me:Apple's Swift
Hey guys,Welcome to 2nd part of Learn With Me:Apple's Swift.In previous tutorial you learned how to write Hello World in Swift and in this tutorial we are going to learn about Variables and Constants in Swift.I will highly recommend to read the first part of Swift tutorial here.So let's get started.
Variables
In programming,variable is a storage area to store data.For eg:
var name="Swift"
Here name is a variable storing "Swift".
Declaring Variables in Swift
In Swift,variables are declared using var keyword.
For example,
var name:Stringvar id:Int
Here,
name is a variable of type String. Meaning, it can only store textual values.
id is a variable of Int type. Meaning, it can only store integer values.
You can assign values to a variable using = operator.
var name:Stringname="Swift"print("My favorite programming language is:",name)
Output
My favorite programming language is:Swift
Note:You can also assign a variable directly without the type annotation.
For example,
var name="Swift"
Constants in Swift
A constant is a special type of variable whose value cannot be changed. A constant is declared using let keyword.For example,
let name="John Doe"print(name)
Output
John Doe
If you'll try to change the value of name then you will get an error.For example,
let name="John Doe"name="Jonathan"print(name)
Output
error: cannot assign to value: 'name' is a 'let' constant
Note:You cannot declare constant without initializing it.
Conclusion
Thanks for reading.I hope it will help you a lot in achieving your dreams.And in next part we are going to learn about Literals in Swift.
Original Link: https://dev.to/thisisanshgupta/learn-with-meapples-swift-528p
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To