📜 ⬆️ ⬇️

Interview - 10 questions about Swift. Part 3

Professional iOS Developer program - 5 months Best Practice on developing mobile applications using Swift 5. 12 OTUS partner companies are waiting for the best graduates in interviews, so we publish a translation of the final article from the iOS Interview Questions series (Swift), where we will look at Dozens of questions, the answers to which will help you in finding employment.


image


1. What are closures and where can they be used?



Follow the link to see all closure syntax options.


2. What are escaping and non-escaping closures (escaping / nonescaping closures)?


@nonescaping (standard) closures:



@escaping (escaping) closures:



3. What are the types of collections available in Swift?


image



4. How is the base class defined in Swift?


In Swift, classes that are not inherited from a base class, and classes that you define without specifying a superclass, automatically become base classes.


5. What are de-initializers and how are they written in Swift?


Deinitializator declared immediately before the release of memory occupied by an instance of the class. Deinitializator spelled with the keyword deinit . It is used if you need to perform any actions or cleaning before freeing the memory occupied by the object.


For example , if you create a custom class to open a file and write some data to it, you will need to close the file before freeing the memory occupied by the class instance.


Deinitializator is written without brackets and does not accept any parameters.


deinit { //   } 

6. When are double question marks “??” used?


This operator is called the nil union operator . It is used to set the default value if the optional is nil.


 let a: String? = nil let b = "nil coalescing operator" let result = a ?? b print(result) //:"nil coalescing operator" 


6. What is the difference between '?' And '!'?


The "?"


  1. When working with optionals, you can put a “?” In front of such command sets as methods, properties, and subscripts.
  2. If the value before “?” Is nil, then everything that comes after “?” Is ignored, and the value of the entire expression becomes equal to nil.
  3. Otherwise, the optional is unpacked, and everything that comes after the “?” Affects the unpacked value.
  4. In both cases, the value of the whole expression is optional.

The "!"


  1. After making sure that the optional contains a value, you can access its base value by adding an exclamation mark (!) At the end of the optional name.
  2. The exclamation mark actually says: “I know that this optional really matters; please use it. "
  3. Use it only if you are absolutely sure that an implicitly unpacked optional matters when you first access it.

7. What is a type alias in Swift?


A type alias declaration enters a named alias of an existing type into a program. Type alias declarations are declared using the typealias keyword.


typealias name = existing type


 typealias StudentName = String let name:StudentName = "Jack" 

You can use typealias for most types in Swift, for example:



8. What is the difference between functions and methods in Swift?


A method is a function associated with a class, structure, or enumeration. This applies to both instance methods and type methods.


The function is declared in the global scope and does not belong to any type.


Functions can be defined outside of classes or within classes / structures / enums, while methods must be defined inside and be part of classes / structures / enumerations.


9. What is the syntax for external parameters?


The external parameter allows us to name the parameters of the function to make their purpose more understandable.


func power (base a: int, exponent b: int) -> int


* Sometimes it is useful to name each parameter when calling a function to indicate the purpose of each argument that is passed to the function.


If you want users of your function to specify the names of the parameters when they are called, define the name of the external parameter for each parameter in addition to the local parameter name. *


10. Can I override structures and enumerations in Swift?


You cannot create a subclass of a structure or enumeration, as well as override them. Because the structure is a type of value, and the compiler must know its exact size at compile time, which overrides make it impossible.


To find the previous parts, follow the links Part 1 , Part 2 , All about closures , All about properties


That's all! We are sure that the transfers will be useful not only for students of the “iOS Developer” course, but also for many Habr users. We wish you all professional success and look forward to the nearest groups of our author's online programs!


')

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


All Articles