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 p…


This content originally appeared on DEV Community and was authored by Ansh Gupta

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:String
var 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:String
name="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.


This content originally appeared on DEV Community and was authored by Ansh Gupta


Print Share Comment Cite Upload Translate Updates
APA

Ansh Gupta | Sciencx (2021-10-15T06:43:37+00:00) Learn with me:Apple’s Swift. Retrieved from https://www.scien.cx/2021/10/15/learn-with-meapples-swift/

MLA
" » Learn with me:Apple’s Swift." Ansh Gupta | Sciencx - Friday October 15, 2021, https://www.scien.cx/2021/10/15/learn-with-meapples-swift/
HARVARD
Ansh Gupta | Sciencx Friday October 15, 2021 » Learn with me:Apple’s Swift., viewed ,<https://www.scien.cx/2021/10/15/learn-with-meapples-swift/>
VANCOUVER
Ansh Gupta | Sciencx - » Learn with me:Apple’s Swift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/15/learn-with-meapples-swift/
CHICAGO
" » Learn with me:Apple’s Swift." Ansh Gupta | Sciencx - Accessed . https://www.scien.cx/2021/10/15/learn-with-meapples-swift/
IEEE
" » Learn with me:Apple’s Swift." Ansh Gupta | Sciencx [Online]. Available: https://www.scien.cx/2021/10/15/learn-with-meapples-swift/. [Accessed: ]
rf:citation
» Learn with me:Apple’s Swift | Ansh Gupta | Sciencx | https://www.scien.cx/2021/10/15/learn-with-meapples-swift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.