Skip to content

Swift Programming

Master Swift Programming with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.

🎓 50 cards ⏱️ ~25 min Advanced
Study Full Deck →
Share: 𝕏 Twitter LinkedIn WhatsApp

🎯 What You'll Learn

Preview Questions

12 shown

What is an Optional in Swift?

Show ▼

An Optional in Swift represents a variable that can hold either a value or nil. It is declared using a ? after the type, e.g., var name: String?. Optionals enforce safe handling of the absence of a value at compile time.

How do you unwrap an Optional using if let?

Show ▼

You use optional binding with if let:
if let unwrapped = optionalValue {
  print(unwrapped)
}

This safely unwraps the optional and binds its value to a new constant within the block's scope.

What is the difference between Optional chaining and forced unwrapping?

Show ▼

Optional chaining uses ?. and returns nil if the optional is nil, failing gracefully.
Forced unwrapping uses ! and crashes at runtime if the optional is nil.
Example: person?.name vs person!.name

What is the nil-coalescing operator in Swift?

Show ▼

The nil-coalescing operator ?? provides a default value when an optional is nil.
Example: let name = optionalName ?? "Unknown"
If optionalName is nil, name is set to "Unknown".

What is an implicitly unwrapped optional?

Show ▼

An implicitly unwrapped optional is declared with ! instead of ?, e.g., var name: String!. It is automatically unwrapped when accessed, but will crash if it is nil. Used when a value is guaranteed to exist after initialization.

What is a Protocol in Swift?

Show ▼

A Protocol defines a blueprint of methods, properties, and other requirements for a particular task.
Example:
protocol Drawable {
  func draw()
}

Classes, structs, and enums can conform to protocols by implementing the required members.

How do you add a default implementation to a protocol?

Show ▼

You use a protocol extension:
protocol Greetable {
  func greet()
}
extension Greetable {
  func greet() { print("Hello!") }
}

Conforming types inherit the default but can override it.

What is protocol composition in Swift?

Show ▼

Protocol composition lets you require a type to conform to multiple protocols using the & operator.
Example: func process(item: Codable & Hashable)
The parameter must conform to both Codable and Hashable.

What is a closure in Swift?

Show ▼

A closure is a self-contained block of functionality that can be passed around and used in your code. Its syntax is:
{ (parameters) -> ReturnType in
  statements
}

Closures can capture and store references to variables from their surrounding context.

What is trailing closure syntax?

Show ▼

Trailing closure syntax allows you to write a closure argument after the function call's parentheses when it is the last parameter.
Example:
numbers.sorted { $0 < $1 }
instead of numbers.sorted(by: { $0 < $1 }).

What does @escaping mean for a closure parameter?

Show ▼

The @escaping attribute indicates that a closure can outlive the function it is passed to, e.g., stored in a property or called asynchronously.
func fetch(completion: @escaping (Data) -> Void)
Non-escaping closures (the default) cannot be stored or called after the function returns.

What are capture lists in closures?

Show ▼

A capture list defines how values are captured inside a closure, helping prevent retain cycles.
Syntax: { [weak self, unowned manager] in ... }
[weak self] – captures self as a weak optional[unowned self] – captures self without increasing the reference count

🎓 Start studying Swift Programming

🎮 Study Modes Available

🔄

Flashcards

Flip to reveal

🧠

Focus Mode

Spaced repetition

Multiple Choice

Test your knowledge

⌨️

Type Answer

Active recall

📚

Learn Mode

Multi-round mastery

🎯

Match Game

Memory challenge

Related Topics in Programming

📖 Learning Resources