Swift Variables

A variable provides us with named storage that our programs can operate. Variable names cannot contain mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters nor can they begin with a number, although numbers may be included elsewhere within the name.

Variable Declaration in Swift

A variable declaration tells the compiler of which type, where and how much to create the storage for the variable. Declarations of variables are made using the var keyword.

Example:
import Cocoa

var firstvar = 68

println(firstvar)

The output of the above code snippet will be:

Output:
68

Type Annotations

Programmers can supply a type annotation when they declare a variable, to be clear about the kind of value(s) that the declared variable can store. The general form of using this variable is:

Syntax:
var variableName:<data type> = <optional initial value>
Example:
import Cocoa

var firstvar = 68

println (firstvar)

var secVar:Float

secVar = 3.14159

println (secVar)

println(" 1st Value \(varA) 2nd Value \(varB) ")

Scroll Back to Top