The Basics of Swift Syntax

Swift is a type-safe language, which means it helps you avoid mistakes by making sure the values you use in your code are always the right type. For example, if a function expects a number, Swift won’t let you accidentally pass in a string. This kind of safety makes your code more predictable and easier to debug.

Here, we’ll walk through how to declare constants and variables, and how to work with strings and comments in a way that keeps your code clean and easy to follow.

Constants

In Swift, constants are values that you set once and don’t change. You create them using the let keyword. Once a constant is assigned a value, that value stays the same throughout the rest of the program. Swift won’t let you change it later, which helps prevent mistakes.

Here’s a simple example:

let birthYear = 1990

In this case, birthYear is a constant. You’ve told Swift, “This will always be 1990”, and Swift makes sure it stays that way.

Constants are helpful for any value that should stay fixed, such as configuration settings, labels, or anything that represents a known truth in your code. This


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *