Variables in Swift

Article by: Manish Methani

Last Updated: October 14, 2021 at 10:04am IST
2 min 12 sec read

Like human memory, Computer memory has millions of cells where it can store data or information. Now to retrieve information from those cells, each cell should be given one name called " variable name" These variables are most useful and can be of different types like integer, float, double, char, and strings.

Declare Variables in Swift

To declare variables in Swift "var" keyword is used. Semicolons are not compulsory in Swift Language. But remember if you add two statements in a single line you have to use ";" as the delimiter to separate them. Otherwise, it shows a syntax error.

Syntax

var variablename = Constant Value

Example

var name = "Manish"

Let's create a sample playground, Goto File > New > Playground > Next.

Example

import UIKit
var a = 20
print(a)

Run the program in the playground.

Output

You can declare multiple variables on a single line, separated by commas:

var x = 0.0, y = 0.0, z = 0.0

Type Annotations

Annotations are used to specify the type to variables. With the help of Type Annotations, it will be clear what kind of value that specific variable can store. And this can be done by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.

Syntax

var variableName: = 

Example

var name: String
name = "Manish"

var varA,varB,varC: Float

How to use print statements in Swift?

//Playground - noun: a place where people can play

import UIKit

var name = "Manish"
var age = 23

print("(name) age is = (age)")

Output

Manish age is = 23

Explore Codzify Courses

PREMIUM

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Course Fees: INR 1299/-

Apply Coupon Code: UNLOCK

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Flutter Mobile App Development Course

Level: Intermediate

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

The Complete Angular Course

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Dart Programming for Absolute Beginners

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Coming Soon
FREE

Next.js course

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Fundamentals of Computer Programming Languages

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Level: Beginners

Course Fees: FREE

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer