Swift Constants

Constants associate a name (such as str, pi num or welcomeMsg) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, whereas a variable can be set to a different value in the future. In other words, Constants refer to fixed values in the Swift program which will not alter during the execution of the program.

Constants can be of any of the basic data types like:

  • integer constants
  • floating constants
  • character constant
  • string literal.

There are enumeration constants as well.

Declaring Constants

Before using the constants, programmers must have to declare them using the keyword let. The declaration is done in the following ways:

Example:
let constantName = <initial value>

Here is a sample Swift Program

Example:
import Cocoa

// First Constant is initializing value

let firstconst = "Karlos"

// Second Constant is initializing value

let secconst = 240.00

println("1st Value \(firstconst) and 2nd Value is \(secconst)")

println("Program Ends")

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.