We’ll quickly move through some of the basics of Swift.
Swift 4 2 Try it Option Online You really do not need to set up your own environment to start learning Swift 4 programming. Reason is very simple, we already have set up Swift 4 environment online, so that you can execute all the available examples online at the same time when you are doing your theory work. Our Swift tutorials help new and experienced developers alike start programming in Swift, the language necessary to develop iOS apps and Mac desktop applications. We offer basic Swift training, covering Swift syntax and general programming concepts, as well as hands-on coding challenges to test your skills and start-to-finish.
3.1 Printing and string interpolation
We can use
print
to print a value and ()
for string interpolation.
3.2 Comments
Single line comments are declared by putting
//
at the beginning of a line.
Learn Swift For Free
Multi-line comments are started using
/*
and ended by */
.
3.3 Basic types
The basic types in Swift are:
3.3.1 Booleans
Booleans are of type
Bool
and can be either true
or false
. Anything in Swift that takes a boolean, must use a real boolean type.
Swift is strict in its treatment of boolean values. Boolean is a type that can (only) be true or false. Any operator or function that expects a boolean must be given a boolean or the type system will complain. Many dynamic languages have a much looser definition of what kind of values can be truthy (for example, in Ruby anything that not nil or false is truthy. So an empty string can be used to represent true) - if you’re coming from one of those languages you’ll have to get used to only using booleans in places where booleans are expected.
Booleans are declared using the keyword
Bool
or by initializing them with true
or false
.
3.3.2 Integers
Int
is the basic Integer type. There are also a number of Integer types of various sizes, both signed and unsigned.Listing 3.1 shows each of the Integer types along with their min and max values. For each of these types you can see their minimum and maximum values using the min
and max
properties.
The full range of Integer types are Int, UInt, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64
3.3.3 Floats and Doubles
Floating point numbers are represented using
Float
and Double
, depending on the precision that you need. Floats are represented using 32bits whereas Doubles are stored using 64 bits. Doubles have precision of at least 15 decimal places, whereas Floats can have as little as 6, depending on the value. Which one you use depends on how much precision you need. Here is an example where we lose precision by assigning a Float with more decimal places than it can handle.
3.3.4 Strings and characters
Learn Swift Programming
Strings and characters are represented by the
String
and Character
types respectively.
By default Swift will infer that something in double quotes is a String. But if we want it to be a character we can explicitly declare it as
Character
type. We can use the +
operator to concatenate two strings and we can use the Strings append
method to concatenate a character to a string.
Both String and Character support unicode.
Figure 3.1 shows what the the above unicode strings look like in a playground.
Learn Swift Coding
Figure 3.1: Characters and Strings using unicode.
Like any other basic type, strings can be either mutable or immutable - depending on whether you declare them using let or var (more on this in chapter 4).
3.4 Some basic operators
The operators for addition, subtraction, multiplication, division and modulo are similar to the ones in most c-like languages.
Assignment uses
=
. Swift also has a number of compound assignment operators.
We can also deconstruct assignment from tuples into vars (we cover tuples in more detail in chapter 8).
Comparison operators are similar in Swift to those in any other c-like language.
Logical operators are stricter than you may be used to. They return a boolean and each part of the expression must evaluate to a boolean. As outlined in Section 3.3.1 these operators must be used with boolean values, and they return a boolean value.
Swift has 2 operators for creating ranges. This is the syntax for creating a range in Swift.
The first version (called the closed range operator) includes 10 in the range. The second version (called the half-open range operator) doesn’t include 10 in the range.
The ternary operator accepts a condition that evaluates to a boolean and then returns either the first or second branch depending on whether the initial expression was true or false.
3.5 Naming things
Swift programs use camel-case for all naming. To summarize:
- CamelCase for everything
- class names and top-level constants should be capitalized (i.e.
ClassName
) - method names, function names and variable names should start with a lower-case letter (i.e.
methodName
)
In going through these examples we’ve already encountered the syntax for declaring constants and variables in Swift, although we haven’t discussed it. In the next chapter we’ll cover that in a bit more detail.
Swift is a new programming language for developing apps for IOS and Mac OS, and it is destined to become the foremost computer language in the mobile and desktop space. This Swift tutorial series will help you to get started in the Swift programming language.
Required Knowledge
To learn Swift Programming language, you haven't needed any previous programming knowledge, but the basic understanding of any other programming languages will help you to understand the Swift programming concepts quickly.
Swift Example
A quick look at the example of Hello, World! In Swift programming. A detailed description is given in the Swift Basic Syntax page.
Program Output:
The above example has been used to print 'Hello World' on the screen.