What is 'Declarations' in Swift?

ios dev·2022년 3월 19일
0

Swift

목록 보기
1/3
post-thumbnail

Declarations

A declaration introduces a new name or construct into your program.
Declaration은 프로그램에 새 이름이나 구조(construct)를 도입하는 것이다.

예를 들면, Declaration을 사용해 함수와 메서드를 도입하거나 변수와 상수를 도입할 수 있고 열거형, 구조체, 클래스 및 프로토콜 타입을 정의할 수 있다.

또, Declaration을 사용하면 기존에 명명된 타입의 동작을 확장(extension)할 수 있고 현재 파일 외부에 선언된 symbol을 import해 해당 symbol에 접근할 수도 있다.

Swift에서 대부분의 Declaration은 Declaration과 동시에 구현되거나 초기화된다는 점에서 Definition이기도 하다.

프로토콜은 구성원을 구현하지 않기 때문에 대부분의 프로토콜은 Declaration일 뿐인데 Swift에서는 구분이 그렇게 중요하지 않기 때문에 Declaration은 Declaration과 Definition을 모두 포함한다.


Import Declaration

import declaration을 사용하면 현재 파일 외부에 선언된 symbol을 사용할 수 있다.

import (module)

Constant Declaration

constant declaration은 프로그램에 상수 이름 값을 도입한다.
constant declaration은 let 키워드로 선언된다.

let (constant name): (type) = (expression)

Variable Declaration

variable declaration은 프로그램에 value라는 변수를 도입하고 var 키워드로 선언된다.

Stored Variables and Stored Variable Properties

var (variable name): (type) = (expression)

Computed Variables and Computed Properties

var (variable name): (type) {
	get {
		(statements)
    }
    set(setter name) {
        (statements)
    }
}

Stored Variable Observers and Property Observers

var (variable name): (type) = (expression) {
    willSet(setter name) {
        (statements)
    }
    didSet(setter name) {
        (statements)
    }
}

Type Alias Declaration

type alias declaration은 기존 타입의 별칭을 프로그램에 도입한다.

typealias (name) = (existing type)

Function Declaration

function declaration은 함수나 메서드를 도입한다.

func [function name](parameters) -> return type {
    statements
}





cf.
https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

0개의 댓글