Swi Interview Questions
43. There is a compile time error in the following code. Are you
able to recognise it and explain why it occurs? What are
some possible solutions?
struct Cat{
}
func showCat(cat: Cat?) {
guard let c = cat else {
print("No presence of cat")
}
print(c)
}
The problem with the given code is that a guard's else block requires an escape path,
which can be achieved by returning, throwing an exception, or calling a
@noreturn.
Adding a return statement is the simplest approach:
func showCat(cat: Cat?) {
guard let c = Cat else {
print("No presence of cat")
return
}
print(c)
}
A fatalError() can also be called which is a
@noreturn function:
struct Cat{
}
func showCat(cat: Cat?) {
guard let c = Cat else {
print("No presence of cat")
fatalError()
}
print(c)
}
Page 36
© Copyright by Interviewbit
Swi Interview Questions
44. This below given difficult code sorts an array of fruits. Write
down ways to code the given logic in a simpler way than the
code given below.
var fruits = ["apples", "oranges", "papaya", "kiwi"]
fruits.sort { (a: String, b: String) -> Bool in
return a < b
}
print(fruits)
There are three ways in which we can simplify the given code snippet:
fruits.sort { (a,b) in return a < b }
fruits.sort { $0 < $1 }
fruits.sort(by: <)
Conclusion:
Apple gadgets are used by billions of people all over the world. The number of iOS
users has been constantly increasing, which is good news for
iOS application
developers
. iOS Swi developers must also keep up with the latest developments in
the iOS Swi community. Make sure you are keeping up with Apple developer news,
podcasts, and blogs. This is unlikely to be a question in an interview, but it
distinguishes you. We hope that the Swi Interview questions answered here are
extremely helpful in studying the fundamentals and advanced topics of iOS Swi . Any
beginner or experienced professional who understands these Swi
and iOS
developer interview questions will be able to pass the interview on the first try. All the
best for your upcoming interviews!
Page 37
© Copyright by Interviewbit