let constant = 10 | 상수 (불변) |
var variable = 10 | 변수 (가변) |
var x: Int = 10 | 명시적 타입 |
var name: String? | 옵셔널 타입 |
var name: String! | 암시적 언래핑 |
typealias Name = String | 타입 별칭 |
Int, Int8, Int16, Int32, Int64 | 정수 타입 |
UInt, UInt8, ... | 부호 없는 정수 |
Float, Double | 부동소수점 |
Bool | 불리언 |
String | 문자열 |
Character | 단일 문자 |
Any | 모든 타입 |
AnyObject | 모든 클래스 타입 |
if let value = optional { } | 옵셔널 바인딩 |
guard let value = optional else { return } | guard let |
optional ?? defaultValue | nil 병합 |
optional?.property | 옵셔널 체이닝 |
optional! | 강제 언래핑 |
if let x, let y { } | 다중 바인딩 (Swift 5.7+) |
var arr: [Int] = [] | 빈 배열 |
var arr = [1, 2, 3] | 배열 리터럴 |
arr.append(4) | 요소 추가 |
arr.insert(0, at: 0) | 인덱스에 삽입 |
arr.remove(at: 0) | 인덱스에서 제거 |
arr.count | 배열 크기 |
arr.isEmpty | 비어있는지 확인 |
arr.first / arr.last | 첫/마지막 요소 |
var dict: [String: Int] = [:] | 빈 딕셔너리 |
var dict = ["a": 1, "b": 2] | 딕셔너리 리터럴 |
dict["key"] = value | 값 설정 |
dict["key"] | 값 얻기 (옵셔널) |
dict.removeValue(forKey: "key") | 값 제거 |
dict.keys / dict.values | 키/값 |
var set: Set<Int> = [] | 빈 집합 |
var set: Set = [1, 2, 3] | 집합 리터럴 |
set.insert(4) | 요소 삽입 |
set.remove(1) | 요소 제거 |
set.contains(1) | 포함 확인 |
set1.union(set2) | 합집합 |
set1.intersection(set2) | 교집합 |
func name() { } | 기본 함수 |
func name(param: Int) -> Int | 반환 값이 있는 함수 |
func name(_ param: Int) | 인수 레이블 없음 |
func name(label param: Int) | 커스텀 인수 레이블 |
func name(param: Int = 0) | 기본 매개변수 |
func name(_ params: Int...) | 가변 매개변수 |
func name(param: inout Int) | inout 매개변수 |
func name() throws -> Int | throwing 함수 |
{ (params) -> ReturnType in } | 전체 클로저 구문 |
{ param in return param * 2 } | 추론된 타입 |
{ $0 * 2 } | 축약 인수 |
arr.map { $0 * 2 } | 후행 클로저 |
@escaping () -> Void | 탈출 클로저 |
@autoclosure | 자동 클로저 |
[weak self] | weak 캡처 |
[unowned self] | unowned 캡처 |
class MyClass { } | 클래스 정의 |
init() { } | 이니셜라이저 |
deinit { } | 디이니셜라이저 |
convenience init() { } | 편의 이니셜라이저 |
required init() { } | 필수 이니셜라이저 |
class Child: Parent { } | 상속 |
override func method() | 메서드 오버라이드 |
final class MyClass { } | final 클래스 |
struct MyStruct { } | 구조체 정의 |
mutating func method() | mutating 메서드 |
MyStruct(prop: value) | 멤버별 이니셜라이저 |
var stored = 0 | 저장 속성 |
var computed: Int { get { } } | 계산 속성 |
lazy var prop = ... | 지연 속성 |
static var prop = ... | 타입 속성 |
willSet { } | 속성 관찰자 |
didSet { } | 속성 관찰자 |
@Published var prop | 게시 속성 |
protocol MyProtocol { } | 프로토콜 정의 |
var prop: Int { get set } | 속성 요구사항 |
func method() | 메서드 요구사항 |
class MyClass: Protocol | 프로토콜 채택 |
protocol P: AnotherProtocol | 프로토콜 상속 |
associatedtype T | 연관 타입 |
@objc optional func method() | 선택적 메서드 |
extension MyType { } | 타입 확장 |
extension MyType: Protocol { } | 프로토콜 준수 |
extension Array where Element: Numeric { } | 조건부 확장 |
enum Direction { case north, south } | 기본 열거형 |
enum Result { case success(Int) } | 연관 값 |
enum Status: Int { case active = 1 } | 원시 값 |
Direction.north | case 접근 |
Status(rawValue: 1) | 원시 값으로 초기화 |
indirect enum Tree { } | 재귀 열거형 |
switch value { case pattern: } | switch 문 |
case let x where x > 0: | where 절 |
case .success(let value): | 값 바인딩 |
case 0...10: | 범위 패턴 |
case (let x, let y): | 튜플 패턴 |
case is MyType: | 타입 캐스팅 패턴 |
fallthrough | 폴스루 |
func fetchData() async -> Data | 비동기 함수 |
let result = await fetchData() | await 호출 |
async let a = fetch() | 동시 바인딩 |
func method() async throws | 비동기 throwing |
try await task | await throwing |
Task { await ... } | 태스크 생성 |
Task.detached { } | 분리 태스크 |
actor MyActor { } | 액터 정의 |
await actor.property | 액터 접근 |
nonisolated func method() | 비격리 메서드 |
@MainActor | 메인 액터 |
@globalActor | 전역 액터 |