Write a post
Published Jan 12, 2017Last updated May 18, 2017

10 Basic Swift Interview Questions to Practice with

10 Basic Swift Interview Questions to Practice with

Swift is the latest programming language released by Apple and meant to replace Objective-C. For many people transitioning to Swift from Objective-C or just learning Swift with a Java background, even those preparing for a junior developer interview, I've written a quick summary of the most important aspects of the Swift language.

Here are ten of the most important aspects of this language that you probably have questions about when learning Swift — I know that I had!

(Similarly, here is a guide for beginners who can't decide which one to learn, Swift or Objective-C?)

You can also use these questions to help you prepare for your junior developer interview. Note that while there is no guarantee that any of these questions will be asked during a technical interview, these questions will help you review your knowledge about Swift!

Ten important aspects of Swift you should know

Question 1: What is the question mark ? in Swift?

Answer: The question mark ? is used during the declaration of a property, as it tells the compiler that this property is optional. The property may hold a value or not, in the latter case it's possible to avoid runtime errors when accessing that property by using ?. This is useful in optional chaining (see below) and a variant of this example is in conditional clauses.

var optionalName : String? = “John"
if optionalName != nil {
    print(“Your name is \(optionalName!)”)
}

Question 2: What is the use of double question marks ???

Answer: To provide a default value for a variable.

let missingName : String? = nil
let realName : String? = “John Doe"
let existentName : String = missingName ?? realName

Question 3: What is the use of exclamation mark !?

Answer: Highly related to the previous keywords, the ! is used to tell the compiler that I know definitely, this variable/constant contains a value and please use it (i.e. please unwrap the optional). From question 1, the block that executes the if condition is true and calls a forced unwrapping of the optional's value. There is a method for avoiding forced unwrapping which we will cover below.

Question 4: What is the difference between let and var in Swift?</div>

Answer*: The let keyword is used to declare constants while var is used for declaring variables.

let someConstant = 10
var someVariable : String

Here, we used the : string to explicitly declare that someVariable will hold a string. In practice, it's rarely necessary — especially if the variable is given an initial value — as Swift will infer the type for you. It is a compile-time error trying to use a variable declared as a constant through let and later modifying that variable.

Question 5: What is type aliasing in Swift?

Answer: This borrows very much from C/C++. It allows you to alias a type, which can be useful in many particular contexts.

typealias AudioSample = UInt16

Question 6: What is the difference between functions and methods in Swift?

Answer: Both are functions in the same terms any programmer usually knows of it. That is, self-contained blocks of code ideally set to perform a specific task. Functions are globally scoped while methods belong to a certain type.

Question 7: What are optional binding and optional chaining in Swift?

Answer: Optional bindings or chaining come in handy with properties that have been declared as optional. Consider this example:

class Student {
 var courses : [Course]?
}
let student = Student()

Optional chaining

If you were to access the courses property through an exclamation mark (!) , you would end up with a runtime error because it has not been initialized yet. Optional chaining lets you safely unwrap this value by placing a question mark (?), instead, after the property, and is a way of querying properties and methods on an optional that might contain nil. This can be regarded as an alternative to forced unwrapping.

Optional binding

Optional binding is a term used when you assign temporary variables from optionals in the first clause of an if or while block. Consider the code block below when the property courses have yet not been initialized. Instead of returning a runtime error, the block will gracefully continue execution.

if let courses = student.courses {
  print("Yep, courses we have")
}

The code above will continue since we have not initialized the courses array yet. Simply adding:

init() { courses = [Course]() }

Will then print out "Yep, courses we have."

Question 8: What's the syntax for external parameters?

Answer: The external parameter precedes the local parameter name.

func yourFunction(externalParameterName localParameterName :Type, ...) { ... }

A concrete example of this would be:

func sendMessage(from name1 :String, to name2 :String) { print("Sending message from \(name1) to \(name2)") }

Question 9: What is a deinitializer in Swift?

Answer: If you need to perform additional cleanup of your custom classes, it's possible to define a block called deinit. The syntax is the following:

deinit { //Your statements for cleanup here }

Typically, this type of block is used when you have opened files or external connections and want to close them before your class is deallocated.

Question 10: How should one handle errors in Swift?

Answer: The method for handling errors in Swift differ a bit from Objective-C. In Swift, it's possible to declare that a function throws an error. It is, therefore, the caller's responsibility to handle the error or propagate it. This is similar to how Java handles the situation.

You simply declare that a function can throw an error by appending the throws keyword to the function name. Any function that calls such a method must call it from a try block.

func canThrowErrors() throws -> String

//How to call a method that throws an error
try canThrowErrors()

//Or specify it as an optional
let maybe = try? canThrowErrors()</pre>

Bonus question: What is a guard statement in Swift?

Guard statements are a nice little control flow statement that can be seen as a great addition if you're into a defensive programming style (which you should!). It basically evaluates a boolean condition and proceeds with program execution if the evaluation is true. A guard statement always has an else clause, and it must exit the code block if it reaches there.

guard let courses = student.courses! else {
    return
}

Reference

The content of this post have been extracted from the official Apple Documentation. You can also practice with these 20 iOS Developer Interview Questions

Discover and read more posts from Hajder Rabiee
get started
Enjoy this post?

Leave a like and comment for Hajder

3
4