📜 ⬆️ ⬇️

Interview - 10 questions about Swift. Part 1

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.
')

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:


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.


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 = “Applefunc changeString(newValue:inout String) { newValue = “Samsungprint(newValue) // Output:Samsung print(value) // Output:Samsung } changeString(newValue:&value) 


What is a 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 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 //Output: Midhun nameAndAge.age //Output: 7 


Unnamed tuple
In this type of tuple, we do not specify names for the elements.

 let nameAndAge = (“Midhun”, 7) Access the values like: nameAndAge.0 //Output: Midhun nameAndAge.1 //Output: 7 


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.

Source: https://habr.com/ru/post/451854/


All Articles