두 값을 비교할 때 사용한다.
var valueA: Int = 3
var valueB: Int = 5
var biggerValue: Int = valueA > valueB ? valueA : valueB // 5
valueA = 0
valueB = -3
biggerValue = valueA > valueB ? valueA : valueB // 0
var stringA: String = ""
var stringB: String = "String"
var resultValue: Double = stringA.isEmpty ? 1.0 : 0.0 // 1.0
resultValue = stringB.isEmpty ? 1.0 : 0.0 // 0.0
var unsignedInteger: UInt8 = 0
let errorUnderflowResult: UInt8 = unsighnedInteger - 1 // 런타임 오류
let underflowValue: UInt8 = unsignedInteger &- 1 // 255
unsignedInteger = UInt8.max // 255
let errorOverflowResult: UInt8 = unsignedInteger + 1 // 런타임 오류
let overflowValue: UInt8 = unsignedInteger &+ 1 // 0
prefix operator **
prefix operator **
prefix func ** (value: Int) -> Int{
return value * value // 25
}
let minusFive: Int = -5
let sqrtMinusFive: Int = **minusFive // 25
print(sqrtMinusFive) // 25
postfix operator **
postfix operator **
postfix func ** (value: Int) -> Int{
return value + 10
}
let valueA: Int = 5
let result: Int = valueA** // 15
print(result) // 15
prefix operator ^^
postfix operator ^^
prefix func ^^ (value: Int) -> Int {
return value * value
}
postfix func ^^ (value: Int) -> Int {
return value + 10
}
let five: Int = 5
let sqrtFivePlusTen: Int = ^^five^^
// 후위 연산인 5 + 10을 하고 난 후 값인 15, 전위 연산인 15 * 15 로 값이 255
infix operator
precedencegroup 우선순위 그룹{
higherThan: 더 낮은 우선순위 그룹 이름
lowerThan: 더 높은 우선순위의 그룹 이름
associativity: 결합 방향(left/right/none)
assignment: 할당방향 사용 (true/ false)
}
// String 타입의 contains(_:) 메서드를 사용하기 위해 Foundation 프레임워크를 임포트 합니다.
import Foundation
infix operator ** : MultiplicationPrecedence
func ** (lhs: String, rhs: String) -> Bool {
return lhs.contains(rhs)
}
let helloJin: String = "Hello Jin"
let Jin: String = "Jin"
let isContainsJin: Bool = helloJin ** Jin