0

Getting started with Swift

559Points

6 years ago

Swift is the future of Apple development, and Xcode is the official IDE to work with it. Both were created to allow better code creation in less time, and in this post, we’ll teach you the basics you need to get started with this interesting new language.

Apple has been using Objective-C to develop all of its products since 1996, but its shortcomings have become increasingly obvious over time. Swift integrates the lessons learned during all these years into a new, much more efficient and safer language.

Initial setup

Before starting with Swift, you need to install Xcode on a computer running Apple OS X. For this, you just need to install the application from the Mac App Store. If necessary, the installation process will guide you through the registration steps.

Xcode Playgrounds

Playgrounds are part of the tools that Xcode offers us, and allow us to constantly test our application fragments. As a matter of fact, Xcode checks your code as you type and displays the result of each line on the right to make development easier and error-free.

In this post, we will use this tool to learn the basics of Swift; starting with creating a new playground on the home screen of Xcode.

To create a new playground just indicate the name you want to assign to it and the platform on which you plan to use it.

A difference with Objective-C is that with Swift it is possible but not mandatory to import libraries or features, so it is not necessary to encapsulate everything in a main () function, which means that our traditional “hello world” looks like this:

Variables and constants

The first concept you need to learn in Swift is the difference between a variable and a constant. As its name indicates, a variable is an object whose value can be rewritten; on the other hand a constant can not change value, so it always retains its initial value. The use of constants helps our application use fewer resources.

In the following example we see how we define variables with var and constants with let; note that the constant returns an error if we try to change its value:

Data types

Variables and constants data types can be implicit or explicit; if the data can be inferred easily and does not have special requirements, we can assign a number or text value to an object and Swift will define the variable automatically. If we want to explicitly assign the variable, we should place : after the variable’s name and declare the data type.

Swift does not allow to mix Int and String data types within a single object; this limitation is one of the security measures that guarantees reliable code.

If we want to convert the type of a variable by declaring it as part of another object, we write the name of the data type we want to convert and put the name of the variable to be converted in brackets.

Since the data type can be inferred, Swift lets us use \ () as an abbreviation to convert the data type of the object depending on the context in which it is located.

Creating lists and dictionaries

To create a list, just put data items in square brackets, separated by commas. For more efficiency and safety, lists can be saved in a variable or in a constant. To access a position in a list, just type the name of the list followed by the position number in square brackets; this way you can extract the value in that position, or if the list is a variable, assign it as shown in the following example.

Dictionaries are similar to lists, except than they use text to indicate a position in a data set instead of a number. Both can be assigned as a variable or as a constant. If we request unregistered data from a list or dictionary, we’ll get a nil value.

Please note that you can create blank lists and dictionaries so you can assign values to them later.

Cycles and conditionals

To iterate through the content of a list we use a for cycle; we start by defining the name of the variable that will temporarily store the value of the current list’s object for each iteration, followed by in and the list’s name. Since the values are fixed in our example, we define our list as a constant which makes it more memory efficient.

Optionals are a special type of variable, since all data on Swift must contain a value; optionals without explicit value return nil as a value.

If you want to use the optional variables as conditionals on an if statement to check if they have value or not, since you need a Boolean data type, you have to assign the possibly empty optional variable as the value of a constant.

To avoid the use of nested if else statements it is recommended to use switch case instead; switch statements support any data type and include a variety of logical comparators.

When we use switch case it’s necessary to define a default procedure which will be called when the input value is not between the expected cases.

To create a cycle that is repeated until a condition is met you should use a while statement. This is useful if we want for example, to create cycles that should be repeated a certain number of times.

Classes and functions

We use func to declare a function, followed by the function name, and in brackets we place the name and type of the arguments that receives our function, using -> to indicate the data type that our function returns.

func NAME (NAME : DATA_TYPE) -> DATA_TYPE {
	...
}

To call our function, we write its name and put in brackets the values of the arguments we want to pass, separated by commas.

A class can contain one or more functions, in addition to items whose values are accessible to all functions in that class. We use class to define it and put all the content between { } braces.

To use a class, assign an instance of it in a variable and use this to change or access the class’s contents.

Further learning

I encourage you to deepen your knowledge about Swift: it offers important advantages compared to C-based languages and it is undoubtedly the future of the entire Apple ecosystem. Learn more about developing in this language with our programming course for iOS with Swift (in Spanish) and do not miss upcoming articles on this blog.

Cesar
Cesar
Escribe tu comentario
+ 2