Beruflich Dokumente
Kultur Dokumente
Animesh Mishra
May 5, 2018 · 5 min read
G enerics enables you to write flexible, reusable functions and types that can
work with any type. You can write code that avoids duplication and expresses
its intent in a clear, abstracted manner.
In below code,generic function for swap two value is used for string and integer. It is
the example of reusable code.
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 1/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
An optional in Swift is a type that can hold either a value or no value. Optional are
written by appending a “?” to any type.
1. Things that can fail (I was expecting something but I got nothing)
2. Things that are nothing now but might be something later (and vice-versa)
A property which can be there or not there, like middleName or spouse in a Person
class.
A method which can return a value or nothing, like searching for a match in an array.
A method which can return either a result or get an error and return nothing, like
trying to read a file’s contents (which normally returns the file’s data) but the file doesn’t
exist.
Delegate properties, which don’t always have to be set and are generally set after
initialization.
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 2/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
For weak properties in classes. The thing they point to can be set to nil at any time.
When you need a way to know when a value has been set (data not yet loaded > the data)
instead of using a separate dataLoaded Boolean.
if the optional contains a ‘value’ then calling its related property, methods and
subscripts returns values
if the optional contains a ‘nil’ value all its its related property, methods and
subscripts returns nil
F orced Unwrapping : It’s the way of extracting the value contained inside an
Optional. This operation is dangerous because you are telling the compiler: I am
sure this Optional value does contain a real value, extract it!
I
read it.
mplicit Unwrapping : When we define an Implicitly unwrapped optional, we
define a container that will automatically perform a force unwrap each time we
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 3/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
name = nil
let player = name //fatal error: unexpectedly found nil while unwrapping an
Optional value
You can unwrap an optional in both a “safe” and “unsafe” way. The safe way is to use
Optional Binding.
Optional binding used to find out whether an optional contains a value, and if so, to
make that value available as a temporary constant or variable. There’s no need to use
the ! suffix to access its value.
G
a method.
uard statement is simple and powerful. It checks for some condition and if it
evaluates to be false, then the else statement executes which normally will exit
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 4/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
B enefit of guard statement is faster execution. A guard block only runs if the
condition is false, and it will exit out of the code block through a control transfer
statement like return , break , continue , or thrown . It provides an early exit and fewer
Use guard if else block is relatively short to reduce nesting and indentation.
9. What is defer ?
D efer statement to execute a set of statements just before code execution leaves
the current block of code.
The defer statement inside the if code block will be executed first. Then it follows a
LIFO pattern to execute the rest of the defer statements.
func doSomething() {
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 5/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
defer { print(“1”)}
defer { print(“2”)}
defer { print(“3”)}
if 1<2 {
defer { print("1<2")}
defer { print(“4”)}
defer { print(“5”)}
defer { print(“6”)}
Output 1<2 6 5 4 3 2 1
10. List out what are the control transfer statements used in Swift?
break — The break statement ends execution of an entire control flow statement
immediately
continue — The continue statement tells a loop to stop what it is doing and start
again at the beginning of the next iteration through the loop.
In swift, fallthrough statement is used to execute next switch case statement event if it is
not matching.
let integerToDescribe = 5
var description = “The number \(integerToDescribe) is”
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += “ a prime number, and also”
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 6/7
22.09.2019 iOS Interview Questions (Swift) — Part 2 - Animesh Mishra - Medium
fallthrough
case 10:
description += “ case 10.”
default:
description += “ an integer.”
}
print(description)// Output:The number 5 is a prime number, and also
case 10.
For other parts follow the link Part1 & Part3..All About Closure & All
About Properties
https://medium.com/@anios4991/ios-interview-questions-swift-part-2-d3a070f9dbd 7/7