Basic Operators에 설명된 연산자 외에도 Swift는 더 복잡한 값 조작을 수행하는 여러 고급 연산자 제공
C & Objective-C에서의 all of the bitwise and bit shifting operators 제공
목차
C의 산술 연산자와 달리 Swift의 산술 연산자는 기본적으로 오버플로우 되지 않음
오버플로우 동장이 트랩되고 오류로 보고됨
오버플로 연산자는 &로 시작
custom type으로 구조체, 클래스, 열거형을 정의할 때 표준 Swift 연산자의 고유한 구현을 제공하는 것 유용할 수 있음
미리 정의된 연산자로 제한되지 않음
이러한 연산자는 미리 정의된 연산자와 마찬가지로 채택할 수 있고 사용자 지정 연산자를 지원하도록 기존 타입을 확장할 수도 있음
You’re not limited to the predefined operators. Swift gives you the freedom to define your own custom infix, prefix, postfix, and assignment operators(할당 연산자 =) , with custom precedence (선행되는 연산자) and associativity values. These operators can be used and adopted in your code like any of the predefined operators, and you can even extend existing types to support the custom operators you define.
If an expression has two operators with similar precedence, the expression is evaluated according to its associativity (either left to right, or right to left). For example,
print(6 * 4 / 3) // 8
Here, operators *
and /
have the same precedence. And, their associativity is from left to right. Hence, 6 * 4
is executed first.
Note: If we want to execute the division first, we can use parentheses as print(6 * (4/3))
.