Constants in Swift

Keep that value fixed!

Constants are treated in different ways by programming languages. Swift is no different!

Image for post
Image for post
Photo by Lisanto 李奕良 on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging


  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Naming conventions are touched upon(guide HERE)


Character: A character, usually associated with a letter of the alphabet

Constant: A value that can not be amended during the normal execution of a program

Compiler: A program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer

lowerCamelCase: A naming convention where the name is formed of multiple words joined as a single word, but the first letter of each word is capitalized. The first letter is in lower case.

Underscore: The _ character, in Swift this generally indicates that a piece of information is missing

URL: Uniform Resource Locator. An address of a web page or resource


Constants can actually make your code safe, because you are declaring to the compiler:

Declare this value, and never let it change. Ever.

So a typical example of this might be a URL that is used for network calls within your application.

let resourceLocation = ""

now trying to change resourceLocation to any other value, like the following:

resourceLocation = "another location"

reveals a rather nasty error

Cannot assign to value: ‘resourceLocation’ is a ‘let’ constant

Ok. That’s us well told

Naming conventions

Constants are much like functions or variables in Swift. Just use any combination of:





However, coding conventions of starting constants with an Underscore can be confusing in Swift, and are not recommended.

In fact, constants should read as nouns and be in lowerCamelCase — but of course this is defined by the style guide that you are following for your coding needs.


You want to write code that is easy to read, and clear to understand. Part of this is using constants correctly.

This is important when you have values in your code that you don’t want to change. These constants have many uses, and prevent your program from acting in unexpected ways and therefore can help provide easily understandable and safe code.

Extend your knowledge

  • Apple cover this in “The Basics” through this link. HERE
  • Apple have more information about naming conventions through this link. HERE

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store