Swift Flashcards

Category sponsor

Swift is a powerful and intuitive programming language developed by Apple for building iOS, macOS, watchOS, and tvOS applications. Introduced in 2014, Swift has quickly become the preferred language for developing apps within the Apple ecosystem. It is designed to be safe, fast, and expressive, making it ideal for both beginners and experienced developers. Swift combines the performance and efficiency of compiled languages with the simplicity and flexibility of scripting languages. Its features like type safety, error handling, and memory management make Swift a robust choice for building secure and reliable applications. With an active community and continuous updates, Swift continues to evolve, providing developers with a modern and powerful toolset for app development.

Our flashcard app contains 109 carefully curated Swift interview questions, complete with comprehensive answers, to effectively prepare you for any interview requiring Swift knowledge. IT Flashcards is not just a tool for job seekers – it’s an excellent way to strengthen and test your knowledge, regardless of your current career plans. Regular use of the app will help you stay updated with the latest trends in Swift and maintain your skills in mobile app development at a high level.

Example Swift flashcards from our app

Download our app from the App Store or Google Play to get more free flashcards or subscribe for access to all flashcards.

What is Swift and what is it used for?

Swift is a modern, statically-typed programming language developed by Apple. Due to its features such as safety, performance, and ease of code writing, it has become one of the most popular languages for developing applications on Apple platforms like iOS, macOS, watchOS, and tvOS.

Swift was designed with simplicity and ease of writing in mind, making it attractive to beginner programmers. The language's syntax is concise, making Swift code easy to read and understand.

Swift implements many features that enhance code safety—for example, strong typing and error handling. As a result, less experienced programmers can avoid many common mistakes, while experienced programmers gain tools for writing more resilient code.

Despite its youth (the first version was released in 2014), Swift has gained significant popularity among developers of applications for Apple platforms. Consequently, Swift is now one of the most important languages in the Apple ecosystem.

What are the main differences between var and let in Swift?

In Swift, both var and let are used to declare variables, but there are some key differences between them:

1. Mutability: Variables declared with var are mutable, which means their value can be changed after they are initialized. On the other hand, variables declared with let are immutable, which means once a value is assigned to them, it cannot be changed.

var mutableVariable = "I can be changed"
mutableVariable = "See, I've changed"

let immutableVariable = "I can't be changed"
immutableVariable = "I'll throw a compile-time error" // This will result in a compile-time error


2. Thread Safety: Immutable variables are safer to use in multithreaded environments. Since their values do not change, there's no need to worry about potential issues related to sharing data between different threads.

3. Design: A general programming principle is to use let instead of var wherever possible to make the code more predictable and easier to understand.

What does guard mean in Swift?

Guard in Swift is a statement that we use when we want certain conditions to be met by our code. If the condition is not met, the code within the curly brackets {} is executed, and the entire function where the `guard` is placed is exited. Otherwise, the code outside the curly brackets {} is executed.

Guard is useful when we want to protect expressions from incorrect values or provide correct values at the beginning of a method. We elevate our application's level of safety by eliminating potential errors.

func isNumberEven(number: Int?) {
    guard let num = number, num % 2 == 0 else {
        print("The provided value is incorrect or is not an even number.")
        return
    }
    print("The number \(num) is even.")
}
isNumberEven(number: nil) // output: The provided value is incorrect or is not an even number.
isNumberEven(number: 3)   // output: The provided value is incorrect or is not an even number.
isNumberEven(number: 2)   // output: The number 2 is even.

In the example above, `guard` checks if the provided number is not nil and if it is even. If either of these conditions is not met, an error message is displayed and the function exits. Otherwise, information that the number is even is displayed.

How does switch work in Swift?

The switch statement in Swift is used to execute different blocks of code depending on the value of a variable or expression.

Example usage of switch:
let direction = "west"

switch direction {
case "north":
    print("You're heading north")
case "south":
    print("You're heading south")
case "west":
    print("You're heading west")
case "east":
    print("You're heading east")
default:
    print("Unknown direction")
}

This code first checks if the value of `direction` is `"north"`, then `"south"`, then `"west"`, and finally `"east"`. If none of these conditions are met, the code goes to the `default` case, which gets executed.

In Swift, there is no need to use `break` at the end of each `case` block, because once a particular block is executed, the program automatically "exits" the switch statement.

It is also important to use the `default` keyword at the end of a switch statement. It is required to handle all possible cases. Otherwise, if none of the `case` blocks are met, the program will throw a compilation error.

An interesting feature of the switch statement in Swift is the ability to check multiple conditions in one `case`, making it more flexible than equivalent constructs in other programming languages. This can be achieved by adding a comma between the conditions to be checked.
let direction = "northwest"

switch direction {
case "north", "northwest", "northeast":
    print("You're heading towards the north")
default:
    print("You're not heading towards the north")
}

Download IT Flashcards App Now

Enhance your Swift knowledge with our flashcards.
From basic programming principles to mastering advanced technologies, IT Flashcards is your passport to IT excellence.
Download now and unlock your potential in today's competitive tech landscape.