You are on page 1of 6

22.09.

2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium

iOS Interview Questions (Swift) —


Part 3
For previous parts follow the link Part1 & Part2.. All About Closure &
All About Properties

Animesh Mishra
May 6, 2018 · 4 min read

1. What is closure, where can we use it ?


They are self contained chunks of code that can be passed around and used in
your code.

Closures in Swift are similar to blocks in C and Objective-C and to lambdas in


other programming languages.

https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 1/6
22.09.2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium

They are almost the same as functions but don’t necessarily have a name.

No need to declare the type of each parameter, if you do so you don’t need to state
the return type of the closure.

Follow the link for all type syntax of closure.

2. What are escaping/ nonescaping closures?


@nonescaping closures: (default closure)

When you are passing a closure in function’s arguments, using it before the
function’s body gets execute and returns the compiler back.

When the function ends, the passed closure goes out of scope and have no more
existence in memory.

@escaping closures: .

When are passing the closure in function’s arguments, using it after the function’s
body gets execute and returns the compiler back.

When the function ends, the scope of the passed closure exist and have
existence in memory, till the closure gets executed. link

3. Mention what are the collection types available in


Swift?

Arrays — are used to store same type of multiple values in ordered manner.

Sets — are used to store same type of distinct values in unordered manner.
https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 2/6
22.09.2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium

Dictionaries — are used to store values with key-value pair association in


unordered manner.

4. How base-class is defined in Swift?


In Swift the classes are not inherited from the base class and the classes that you define
without specifying its super class, automatically becomes the base-class.

5. What is de-initializer and how it is written in Swift?


A de-initializer is declared immediately before a class instance is de-allocated. You
write de-initializer with the deinit keyword. Use it if you need to do some action or
cleanup before deallocating the object.

For example, if you create a custom class to open a file and write some data to it, you
might need to close the file before the class instance is deallocated.

De-initializer is written without any parenthesis, and it does not take any parameters.

deinit {
// perform the deinitialization
}

6. What is the use of double question marks ‘??’ ?


This operator is called as nil coalescing operator.We used it to give a default value
when an optional is nil.

let a: String? = nil


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

a ?? b unwraps an optional a if it contains a value, or returns a default value b if a is


nil.

The expression a is always of an optional type. The expression b must match the
type that is stored inside a.

https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 3/6
22.09.2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium

6. What is the difference between ‘?’ and ‘!’ ?


“?” mark

1. When working with optional values, you can write ‘?’ before operations like methods,
properties, and subscript.

2. If the value before the ‘?’ is nil, everything after the ‘?’ is ignored and the value of the
whole expression is nil.

3. Otherwise, the optional value is unwrapped, and everything after the ‘?’ acts on the
unwrapped value.

4. In both cases, the value of the whole expression is an optional value.

“!” Mark

1. Once you’re sure that the optional does contain a value, you can access its underlying
value by adding an exclamation mark (!) to the end of the optional’s name.

2. The exclamation mark effectively says, “I know that this optional definitely has a value;
please use it.”

3. Only use in case when you are absolutely certain that the implicitly unwrapped optional
has a value when it is first accessed.

7. What is type aliasing in Swift?


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

typealias name = existing type

typealias StudentName = String

let name:StudentName = "Jack"

In Swift, you can use typealias for most types. They can be either:

Built-in types (for.eg: String, Int)

https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 4/6
22.09.2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium

User-defined types (for.e.g: class, struct, enum)

Complex types (for e.g: closures)

8. What is the difference between functions and methods


in Swift?
Method — is a function that’s associated with a class, struct, or enum. This goes for
both instance and type methods.

Function — is declared in the global scope and doesn’t belong to any type.

Functions could be defined outside of classes or inside of classes/structs/enums, while


Methods have to be defined inside of and part of classes/structs/enums.

9. What’s the syntax for external parameters?


Extenal Parameter — allow us to name a function parameters to make their purpose
more clear.

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

Sometimes it’s useful to name each parameter when you call a function, to indicate the
purpose of each argument you pass to the function.

If you want users of your function to provide parameter names when they call your
function, define an external parameter name for each parameter, in addition to the local
parameter name.

10. Can you override structs and enums in Swift?


We Can’t subclass struct / enum, not possible to override. Because, struct is value type
and compiler need to know exact size of struct at compile time which is not possible.

For previous parts follow the link Part1 & Part2..All About Closure & All
About Properties

Swift iOS iOS App Development Swift Programming iOS Apps

About Help Legal


https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 5/6
22.09.2019 iOS Interview Questions (Swift) — Part 3 - Animesh Mishra - Medium
bout ep ega

https://medium.com/@anios4991/ios-interview-questions-swift-part-3-c0e326bd2e66 6/6

You might also like