Swift
Swift is Apple’s modern, type-safe language for iOS, macOS, watchOS, tvOS, and server apps.
Keywords
- associatedtype, class, deinit, enum, extension, func, import, init, infix, let, operator, postfix, prefix, protocol, static, struct, subscript, typealias, var.
- Control flow: break, case, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, switch, throw, throws, try, where, while.
- Access control: open, public, internal, fileprivate, private.
- Concurrency: async, await, actor, nonisolated, isolated.
Language Features
- Protocol-oriented programming with protocol extensions.
- Value semantics through structs and enums; classes for reference semantics.
- Optionals (`String?`) eliminate null pointer crashes.
- Pattern matching with `switch`, `if case`, `guard case`.
- Result builders power DSLs like SwiftUI.
- Async/await and actors (Swift 5.5+) simplify concurrency.
Standard Library & Frameworks
- Foundation adds dates, networking, localization.
- SwiftUI and UIKit/AppKit for UI development.
- Combine handles reactive streams; SwiftData and CoreData persist models.
- Server stacks: Vapor, Kitura, Hummingbird, AsyncHTTPClient.
Operators
- Arithmetic: + - * / %
- Comparison: == != < > <= >=
- Logical: && || !, Nil-coalescing: ??
- Range: ..< (half-open), ... (closed)
- Optional chaining: ?. and try? for graceful error handling.
Tooling
- Xcode and Swift Playgrounds for IDE and prototyping.
- Swift Package Manager (`swift build`, `swift test`) manages dependencies.
- SwiftLint enforces style; SwiftFormat auto-formats.
- Instruments and Xcode Organizer profile memory, energy, and performance.
Best Practices
- Use `guard` to exit early and keep happy-path indentation shallow.
- Prefer structs with immutable properties; leverage copy-on-write.
- Annotate concurrency domains clearly with `@MainActor` and Task priorities.
- Adopt unit/UI tests with XCTest and snapshot frameworks.