Salute, habrovchane! May ended, and we continue to launch new courses. This article is timed to the start of the
course "iOS-developer" . Training will begin on May 28, and until then you have the opportunity to learn more about Swift and its features. This is the first of three articles, where the interview format examines the merits and features of the Swift language, in which we teach how to create iOS applications on our courses.

What is Swift and how is it good?
Swift is a universal and multi-paradigm programming language developed by Apple for developing solutions for iOS, macOS, watchOS, tvOS and even Linux.
')
- Readability - Swift has a very clean and simple syntax that is easy to write and read.
- Easy support - the output is much less code and levels of inheritance, and the whole project turns into one swift-file.
- Secure platform - compile and fix errors right while writing code.
- High speed - an incredibly fast and productive LLVM compiler converts Swift code into native code in order to get the most out of the devices. The syntax itself and the standard library are also optimized for fast work.
- Support for dynamic libraries .
- Open source .
What is the difference between a class and a structure?
In Swift, there are four major differences between class and structure. Classes have some features that structures do not have:
- Type casting - allows you to check and interpret classes at run time.
- Link counting - allows you to use more than one link for each class instance.
- Inheritance - allows one class to inherit the characteristics of another.
- De - initializers allow each instance of a class to release all resources assigned to it.
When structures are transmitted in code, they are always copied without reference counting. Instances of the structure are always passed by value, and instances of the class are always by reference.
When to use the class, and in which - the structure?
As a simple cheat sheet: structures should be used when one or more of the following conditions are met.
- The task of the structure is to encapsulate some relatively simple data values;
- Encapsulated values can be expected to be copied, not referenced;
- Properties stored in a structure are themselves types of values that are also copied, not referenced;
- The structure must not inherit the properties and behavior of another existing type.
In other cases, use the classes: define the class, create an instance to manage and pass by reference.
How to transfer variables as links?
A variable can be passed as a reference using the
inout parameter .
Inout means that changing a local variable will also change the parameters passed.
var value: String = “Apple” func changeString(newValue:inout String) { newValue = “Samsung” print(newValue)
What is a module?
- A module is a separate unit in the distribution of a code;
- A platform or application that is created and distributed as a separate unit and can be imported by another module;
- Each build target — an application package or framework in Xcode is treated as a separate module.
What is the difference in access levels in Swift?
In Swift, there are five different access levels for entities in the code:
- Open Access - open access classes can be subclasses or be redefined by subclasses in the module where they are defined, or in any other that imports the module where they are defined;
- Public access - classes with public access can be subclasses or be redefined by subclasses only within the module where they are defined;
- Internal access - entities can be used in any source file from the defining module, but not in the source file outside this module;
- File-private access — use of entities is limited to its own defining source file;
- Private access - the use of entities is limited to the nested declarations and extensions of this ad, which are in the same file.
Open access is the highest and least limited level, and private - the lowest and, accordingly, the most limited. By default, all entities in the code have an internal access level.
What is deferred initialization?
Delayed initialization is a technique to delay the creation of an object or the execution of another process until this process becomes necessary. Delay can only be used with classes and structures. However, it should be understood that the
lazy property is not safe because it is not initialized automatically.
You always need to declare the
lazy property as a variable using
var . Constant properties must always have a value until initialization is completed, so they cannot be deferred.
What is a tuple?
A tuple is a group of zero or more values that are represented as one value. They are usually used to return multiple values from a call function. Swift has two kinds of tuples.
Native tuple let nameAndAge = (name:”Midhun”, age:7) Access the values like: nameAndAge.name
Unnamed tupleIn this type of tuple, we do not specify names for the elements.
let nameAndAge = (“Midhun”, 7) Access the values like: nameAndAge.0
What is a listing?
The enumeration determines the general type for a group of related values and makes it possible to work safely with these values in code. Unlike C and Objective-C, Swift does not assign integer default values to enumerations when creating them.
What are related values?
Related values are very similar to variables associated with one of the enumeration cases.
enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String) }
We define the type of the
Barcode enumeration, which can take either the value upc with an associated value of type
(Int, Int, Int, Int) , or the value
qrCode with the associated value of type
String .
Sometimes the ability to store related values of other types alongside case values can be useful. This allows you to store additional user information along with the value of the case and allows you to change this information each time you use this case in code.
The end of the first part.
The second part of.
We invite everyone to participate in a
free webinar , where we will tell you what this course will teach you.