Swift’s Basics

Vicente Soriano
3 min readAug 19, 2019

Like any other language, if you want to start using Swift, a great foundation is learning the basics. Declaring variables, functions, and understanding data types are essential, especially if it’s the first programming language you’re trying to learn. If it is, congratulations on taking the first steps to becoming a developer!

Data Types

In Swift, there are many different types. A few of the most common types are Strings, Integers, Doubles, and Booleans. There also some collection types like Arrays and Dictionaries in case you’d like to have a list.

Strings are a great way to represent sentences or statements.

Integers represent whole numbers.

Doubles should represent numbers with decimal points.

Booleans represent a true or false value.

Arrays represent a collection of items.

An array containing a list of Strings

Because Swift is considered a type-safe language, if you try to add an Integer with a Double, Strings, or any other type that isn’t an Integer, you will get an error.

Declaring Variables

When declaring a variable, the first thing you should consider is whether the value will change. If you think the value might change, I’d recommend declaring the variable using the var keyword. In the following example, I declared a variable “numberOfSteps” and gave it the value 30(an Integer). There’s a great chance the value will increase in the future so I used the var keyword.

If you’re going to declare a variable that won’t change, like a birthday, you should use the let keyword.

If you try to change the value of birthday after declaring it’s value (“September 25”) you will get an error that’s basically telling you that birthday can’t be changed because you used the let keyword to declare it.

If you’re wondering why the keywords var or let were only used once, it’s because you only need to use them when assigning a value for the first time.

Declaring Functions

Like data types and variables, functions are also essential in Swift and every other programming language. They are also a great way to reduce repetition. In Swift, they are declared using the keyword func. Below, I’m declaring a function named addToFavoriteArtists. It takes in an artist(that is expected to be a String) and adds it to my list of favorite artists.

I just remembered listening to I Ran(So Far Away) by Flock of Seagulls for the first time during Grand Theft Auto: Vice City’s commercial. Let’s go ahead and add them to my list of favorite artists.

Feel free to visit Swift’s Language Guide for more in depth information and remember, the best way to learn is through practice. Cheers!

--

--