iOS Development
118 flashcards by MentisLibrary
Swift essentials, SwiftUI state and navigation, UIKit view controllers and Auto Layout, Swift concurrency, networking and persistence, app lifecycle, Xcode tooling, testing and App Store distribution, for developers shipping their first iOS apps.
- What is an optional in Swift?
- A type that either holds a value or is nil, written String?; the compiler forces you to unwrap it before use, which is how Swift makes nil handling explicit.
- When would you use guard instead of if let?
- When a function should exit early unless a condition holds: guard unwraps into the enclosing scope and its else branch must leave (return, throw, continue).
- struct vs class in Swift
- Structs are value types copied on assignment and cannot inherit; classes are reference types shared through pointers, support inheritance and deinit. Swift favors structs for models and views.
- let vs var
- let declares a constant that is assigned once; var declares a variable. Prefer let everywhere the value never changes; the compiler warns when a var is never mutated.
- What is a protocol in Swift?
- A blueprint of required properties and methods that structs, classes and enums adopt; extensions can supply default implementations, which is the basis of protocol-oriented programming.
- How are closures written in Swift?
- Self-contained blocks of code with parameters and a return type inside braces: { (a: Int) -> Int in a * 2 }. Trailing closure syntax lets the last closure argument sit outside the parentheses.
- What does [weak self] do in a closure?
- It captures self as a weak reference so the closure does not keep the object alive, breaking the retain cycle that forms when an object stores a closure that references the object.
- How do enums with associated values work?
- Each case can carry its own payload, and a switch extracts it with pattern matching; the switch must be exhaustive.
- How does Swift error handling work?
- Functions that can fail are marked throws, callers use try inside do/catch; try? turns an error into nil and try! crashes on error. Any type conforming to Error can be thrown.
- What are generics in Swift?
- Type parameters that let a function or type work with any type while staying type-safe, optionally constrained with where or a protocol bound.
- some vs any for a protocol type
- some P is an opaque type: one concrete type the compiler knows but the caller does not see; any P is an existential box that can hold different conforming types at runtime.
- What are property observers?
- willSet and didSet blocks on a stored property that run just before and after its value changes, useful for updating UI or validating.
- Computed property vs stored property
- A stored property holds a value; a computed property has get (and optionally set) and calculates its value every time it is read.
- How does ARC manage memory?
- Automatic Reference Counting frees a class instance when its strong reference count reaches zero; weak and unowned references do not count, and deinit runs on deallocation.
- Name Swift's access levels
- open, public, internal (the default, visible within the module), fileprivate and private (visible within the enclosing declaration and its extensions in the same file).
- What does defer do?
- It schedules a block to run when the current scope exits, however it exits, which is the idiomatic way to guarantee cleanup.
- What is a property wrapper?
- A type marked @propertyWrapper that wraps a property's storage with extra behavior; SwiftUI's @State, @Binding and @AppStorage are property wrappers.
- How does Codable work?
- Codable is Encodable & Decodable; the compiler synthesizes encoding for types whose properties are all Codable, and JSONDecoder/JSONEncoder convert to and from JSON.
- Which protocols does the compiler synthesize for a struct?
- Equatable, Hashable and Codable, as long as every stored property already conforms; you only declare the conformance.
- What is the View protocol in SwiftUI?
- The protocol every SwiftUI view adopts, requiring a body computed property that returns some View describing the UI from the current state.
- What is @State?
- A property wrapper for a view's own local source of truth; SwiftUI stores the value outside the struct so it survives re-creation, and changing it re-renders the body.
- What is @Binding?
- A two-way reference to state owned elsewhere, so a child view can read and write the parent's value; the parent passes $value (the projected value of @State).
- @StateObject vs @ObservedObject
- Both observe an ObservableObject; @StateObject creates and owns the object once for the view's lifetime, while @ObservedObject merely observes an object passed in from outside.
- What does the @Observable macro change?
- On iOS 17 it replaces ObservableObject: every stored property is tracked automatically, views re-render only for properties they actually read, and no @Published is needed.
- How does @Environment work?
- It reads a value SwiftUI injects down the view tree by key path, such as \.dismiss, \.colorScheme or \.locale, and re-renders when it changes.
- Why does modifier order matter in SwiftUI?
- Each modifier wraps the view in a new view, so .padding().background(.blue) colors the padded area while .background(.blue).padding() colors only the content and pads outside it.
- Name SwiftUI's basic stack containers
- VStack (vertical), HStack (horizontal) and ZStack (layered), plus Spacer to push content apart and LazyVStack/LazyHStack inside a ScrollView for long content.
- How do List and ForEach identify rows?
- Each element needs a stable identity: conform the model to Identifiable or pass id: \.someKey so SwiftUI can animate inserts, moves and deletes correctly.
- How does navigation work with NavigationStack?
- NavigationStack (iOS 16) holds a path of values; NavigationLink(value:) pushes a value and .navigationDestination(for:) maps each value type to a destination view.
- How is a tab bar built in SwiftUI?
- With TabView, giving each child a .tabItem containing a Label; a selection binding lets you switch tabs programmatically.