Variables, constants, and types of data in Swift

Educativo Courses
A free video tutorial from Educativo Courses
Your way to IT professional
3.9 instructor rating • 12 courses • 21,436 students

Learn more from the full course

Swift from scratch - learn programming on iOS

Learn the basics of new language! Create fast, safe and interactive apps with no programming experience!

01:28:35 of on-demand video • Updated May 2020

  • The Swift syntax
  • Basic programming techniques and constructions
  • Key elements of the language
  • Work in Xcode
  • Build your first application using Swift
English [Auto] Now let's talk some more about variables. We start the declaration of a variable from the keyword var followed by the name of the variable a colon and the type of the variable a colon and swift usually means that the expression on the left is of the type specified after the colon. For instance a declaration of the variable that stores temperature in Centigrade could look like this bar Celsius float. In this case float represents a type of floating point data that represents real numbers. The basic data types in swift include strings which represent a line of characters. In other words the text characters which represent a single character in swift is a single Unicode character. In other especially older languages it was a single ASCII character and the simplest number type which represents integer numbers. Float the number type we mentioned in the previous example and it represent real numbers. Double A much more accurate number type also intended for working with real numbers. Bhool a logical type that represents logical values one that is true or T and oh that is false or nil. Apart from variables we can also define constants in swift a declaration of a constant is very similar to a variable declaration. We started from the let keyword followed by its name a colon its type and after an equal sign its value. For instance a declaration of the PIII constant could look like this. Let PIII colon double equal sign value PIII we'll return to the line while to demonstrate. Another interesting property of Swift we should use constants whenever we know that a given value mustn't change once it's assigned. If we accidentally tried to change the value of a constant the compiler will warn us to spare us some errors which could be hard to detect. It will also optimize our code the right labeling of values will also make our code easier to read and make the intentions of the programmer clear. Such a code is also safer which is particularly important in the case of multi-threaded programs. Now let's look at several additional examples of declarations of variables and constants in swift. Here we can see the following declarations. A constant that is the capital of Poland. A variable that is a population of Poland in thousands and a logical variable that is an even number. And again the P constant all the values are initialized with the value present on the right of the equal sign. The most interesting example is probably the declaration of the constant in swift. We can use almost any Unicode character in the variable name. You can even use emoticons so the notation we presented here is by all means correct. In many cases there is no need to specify the variable type. Based on the declared default value Swift is usually able to correctly determine it on its own. This is called type inference. We can simplify our declarations as illustrated in the slide.