let constant = 10 | Constant (immutable) |
var variable = 10 | Variable (mutable) |
var x: Int = 10 | Explicit type |
var name: String? | Optional type |
var name: String! | Implicitly unwrapped |
typealias Name = String | Type alias |
Int, Int8, Int16, Int32, Int64 | Integer types |
UInt, UInt8, ... | Unsigned integers |
Float, Double | Floating point |
Bool | Boolean |
String | String |
Character | Single character |
Any | Any type |
AnyObject | Any class type |
if let value = optional { } | Optional binding |
guard let value = optional else { return } | Guard let |
optional ?? defaultValue | Nil coalescing |
optional?.property | Optional chaining |
optional! | Force unwrap |
if let x, let y { } | Multiple binding (Swift 5.7+) |
var arr: [Int] = [] | Empty array |
var arr = [1, 2, 3] | Array literal |
arr.append(4) | Append element |
arr.insert(0, at: 0) | Insert at index |
arr.remove(at: 0) | Remove at index |
arr.count | Array count |
arr.isEmpty | Check if empty |
arr.first / arr.last | First/last element |
var dict: [String: Int] = [:] | Empty dictionary |
var dict = ["a": 1, "b": 2] | Dictionary literal |
dict["key"] = value | Set value |
dict["key"] | Get value (optional) |
dict.removeValue(forKey: "key") | Remove value |
dict.keys / dict.values | Keys/values |
var set: Set<Int> = [] | Empty set |
var set: Set = [1, 2, 3] | Set literal |
set.insert(4) | Insert element |
set.remove(1) | Remove element |
set.contains(1) | Check contains |
set1.union(set2) | Union |
set1.intersection(set2) | Intersection |
func name() { } | Basic function |
func name(param: Int) -> Int | Function with return |
func name(_ param: Int) | No argument label |
func name(label param: Int) | Custom argument label |
func name(param: Int = 0) | Default parameter |
func name(_ params: Int...) | Variadic parameter |
func name(param: inout Int) | In-out parameter |
func name() throws -> Int | Throwing function |
{ (params) -> ReturnType in } | Full closure syntax |
{ param in return param * 2 } | Inferred types |
{ $0 * 2 } | Shorthand argument |
arr.map { $0 * 2 } | Trailing closure |
@escaping () -> Void | Escaping closure |
@autoclosure | Auto closure |
[weak self] | Weak capture |
[unowned self] | Unowned capture |
class MyClass { } | Class definition |
init() { } | Initializer |
deinit { } | Deinitializer |
convenience init() { } | Convenience initializer |
required init() { } | Required initializer |
class Child: Parent { } | Inheritance |
override func method() | Override method |
final class MyClass { } | Final class |
struct MyStruct { } | Struct definition |
mutating func method() | Mutating method |
MyStruct(prop: value) | Memberwise initializer |
var stored = 0 | Stored property |
var computed: Int { get { } } | Computed property |
lazy var prop = ... | Lazy property |
static var prop = ... | Type property |
willSet { } | Property observer |
didSet { } | Property observer |
@Published var prop | Published property |
protocol MyProtocol { } | Protocol definition |
var prop: Int { get set } | Property requirement |
func method() | Method requirement |
class MyClass: Protocol | Adopt protocol |
protocol P: AnotherProtocol | Protocol inheritance |
associatedtype T | Associated type |
@objc optional func method() | Optional method |
extension MyType { } | Type extension |
extension MyType: Protocol { } | Protocol conformance |
extension Array where Element: Numeric { } | Conditional extension |
enum Direction { case north, south } | Basic enum |
enum Result { case success(Int) } | Associated values |
enum Status: Int { case active = 1 } | Raw values |
Direction.north | Access case |
Status(rawValue: 1) | Init from raw |
indirect enum Tree { } | Recursive enum |
switch value { case pattern: } | Switch statement |
case let x where x > 0: | Where clause |
case .success(let value): | Value binding |
case 0...10: | Range pattern |
case (let x, let y): | Tuple pattern |
case is MyType: | Type casting pattern |
fallthrough | Fall through |
func fetchData() async -> Data | Async function |
let result = await fetchData() | Await call |
async let a = fetch() | Concurrent binding |
func method() async throws | Async throwing |
try await task | Await throwing |
Task { await ... } | Create task |
Task.detached { } | Detached task |
actor MyActor { } | Actor definition |
await actor.property | Access actor |
nonisolated func method() | Non-isolated method |
@MainActor | Main actor |
@globalActor | Global actor |