Swift is a coding language developed by Apple that’s used in iOS and macOS. It’s easy to read and maintain, making it a great first language. Once you’ve mastered Swift, your next step is to land a job where you can put your skills to work.
Potential employers that use Swift need to know that you’re proficient with the language. So, they may ask questions about its features and concepts during your interview. They may also ask you to use Swift to solve a coding problem during a technical interview. To help you prepare, here are 10 interview questions that’ll test your understanding of Swift.
10 Swift interview questions
While it’s impossible to know exactly what your interviewer will ask, the questions below will give you a solid foundation. They’ll give you a sense of what you’ll need to explain and gauge your familiarity with Swift’s underlying features.
1. Explain the similarities and differences between class and structure
Both structures and classes:
- Define properties to store values
- Define initializers to set up their initial state
- Can be extended
- Define subscripts to provide access to their values using subscript syntax
- Can conform to protocols
- Can work with generics
Classes can do some things that structures can’t. For example, they can inherit other classes’ characteristics and free up assigned resources by deinitializers.
The primary difference between the two is that classes are reference types and structures are value types. Reference types share one copy of data, so when one instance is changed, the other is changed as well. Value types make unique copies of data, so if one instance is changed, the other remains unchanged.
2. Explain the difference between nil and .none
This one’s a bit of a trick question. There’s no difference between nil and .none. Still, you may want to note that nil is used more frequently.
3. What is an optional? What problems do optionals solve?
An optional can either have value or be nil. They can be used for both reference and value types. Objective-C, a precursor to Swift, only allowed the absence of value for reference types. Swift extends that to value types.
Optionals also make your code safer and less likely to fail. They’re written by adding a question mark after the type annotation.
4. What are generics? What purpose do they serve?
Generics allow you to write reusable functions and types that can work with any type. They allow you to write code that avoids duplication. Swift’s standard library uses generics heavily. For example, Array and Dictionary are both generics.
5. Explain what a protocol is in Swift
According to Swift.org, a protocol “defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.” Other classes adopt the protocol and provide implementation. A class that satisfies the rules of a protocol conforms to that protocol.
6. Explain the difference between raw values and associated values in Swift enumerations
An enumeration is a collection of values that belong together. Raw values are used to associate constant values to enum cases. An associated value is used to bind arbitrary data to an enum case.
7. What’s the difference between static and class modifiers?
The static modifier makes a property or function static, which means it’s not overridable and can’t be changed by subclasses. The class modifier can be overridden by a function or property when needed.
8. What is a module?
A module is a single unit of code distribution. Swift treats each build target, like frameworks, as a separate module with its namespace and access controls.
9. What are the access levels in Swift?
Swift has 5 access levels:
- Open access: This is the least restrictive access level. It enables classes and methods to be subclassed and overridden within the same module and outside the module.
- Public access: This level makes an entity accessible to other parts of code inside the same module and other modules.
- Internal access: This is the default access level given to all entities unless you mark it otherwise. Entities marked as internal access are accessible in the scope of their defining module but inaccessible to any source files outside the module.
- File-private access: Entities marked as file-private can be seen and accessed by any other entity inside the same source file.
- Private access: This is the most restrictive access level. Entities marked as private are only accessible inside their defining type or source file.
10. What are the control transfer statements in Swift?
Control transfer statements are keywords that allow you to transition control from one piece of code to another. Swift has 5 control transfer statements:
- Break: This ends the program execution of a loop, if statement, or switch statement.
- Continue: This ends the program execution of the current iteration of a loop statement and returns control to the next iteration of the loop.
- Fallthrough: This causes program execution to continue from one case in a switch statement to the next case.
- Return: This causes program execution to return to the calling function or method.
- Throw: This is used in propagating errors using throw functions.
More Swift interview help
Preparing for an interview can be stressful. Reviewing these questions can help, but if you need a little more assistance, we can help.
If you need a refresher, we have a beginner Learn Swift course. It covers:
- Conditionals and logic
- Arrays and sets
Our Learn Immediate Swift course covers more advanced topics, including:
- Properties and access control
- Protocols and generics
If you’d like to spruce up your portfolio and get more hands-on experience, we offer two Skills Paths that will help.Our Build iOS Apps with SwiftUI walks you through building an iPhone app from start to finish. Build an Intermediate iOS App with SwiftUI walks you through creating a fully-featured app with more complex user interfaces.