Swift

[Swift] ์‚ฌ์šฉ์ž ์ •์˜ ์—ฐ์‚ฐ์ž : ์ „์œ„, ์ค‘์œ„, ํ›„์œ„ ์—ฐ์‚ฐ์ž

yevdev 2022. 3. 15. 22:01

๐ŸŽ€ ํ‚ค์›Œ๋“œ ์ •๋ฆฌ

  • prefix : ์ „์œ„ ์—ฐ์‚ฐ์ž
  • postfix : ํ›„์œ„ ์—ฐ์‚ฐ์ž
  • infix : ์ค‘์œ„ ์—ฐ์‚ฐ์ž
  • operator : ์—ฐ์‚ฐ์ž
  • associativity : ์—ฐ์‚ฐ์ž ๊ฒฐํ•ฉ๋ฐฉํ–ฅ
  • precedence : ์šฐ์„ ์ˆœ์œ„

 

1๏ธโƒฃ ์ „์œ„ ์—ฐ์‚ฐ์ž ์ •์˜์™€ ๊ตฌํ˜„

** ๋ฅผ ์ „์œ„ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•ด๋ณด์ž

prefix operator **

prefix func ** (value: Int) -> Int {
    return value * value
}

let minusFive: Int = -5
let sqrtMinusFive: Int = **minusFive

print(sqrtMinusFive)

// 25

! (๋Š๋‚Œํ‘œ) ๋ฅผ ๋ฌธ์ž์—ด์—๋„ ์‚ฌ์šฉํ•ด๋ณด์ž

prefix func ! (value: String) -> Bool {
    return value.isEmpty
}

var stringValue: String = "yejin"
var isEmptyString: Bool = !stringValue

print(isEmptyString) // false

stringValue = ""
isEmptyString = !stringValue

print(isEmptyString) // true

 

2๏ธโƒฃ ํ›„์œ„ ์—ฐ์‚ฐ์ž ์ •์˜์™€ ๊ตฌํ˜„

** ์„ ํ›„์œ„ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•ด๋ณด์ž

postfix operator **

postfix func ** (value: Int) -> Int {
    return value + 10
}

let five: Int = 5
let fivePlusTen: Int = five**

print(fivePlusTen)

// 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**

print(sqrtFivePlusTen)

// (10+5)*(10+5) == 225

 

 

3๏ธโƒฃ ์ค‘์œ„ ์—ฐ์‚ฐ์ž ์ •์˜์™€ ๊ตฌํ˜„

์ค‘์œ„ ์—ฐ์‚ฐ์ž๋Š” ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์„ ๋ช…์‹œํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน ์ •์˜ ๋ฐฉ๋ฒ•

precedencegroup ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน ์ด๋ฆ„ {
    higherThan: ๋” ๋‚ฎ์€ ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน ์ด๋ฆ„
    lowerThan: ๋” ๋†’์€ ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน ์ด๋ฆ„
    associativity: ๊ฒฐํ•ฉ๋ฐฉํ–ฅ(left/right/none)
    assignment: ํ• ๋‹น๋ฐฉํ–ฅ ์‚ฌ์šฉ(true/false)
}
  • associativity : ๊ฒฐํ•ฉ๋ฐฉํ–ฅ์„ ๋ช…์‹œํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ none!
  • assignment : ์˜ต์…”๋„ ์ฒด์ด๋‹๊ณผ ๊ด€๋ จ๋œ ์‚ฌํ•ญ
    • true: ํ•ด๋‹น ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ํ•  ๋•Œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ• ๋‹น ์—ฐ์‚ฐ์ž์™€ ๋™์ผํ•œ ๊ฒฐํ•ฉ๋ฐฉํ–ฅ ๊ทœ์น™์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ์Šค์œ„ํ”„ํŠธ์˜ ํ• ๋‹น ์—ฐ์‚ฐ์ž๋Š” ์˜ค๋ฅธ์ชฝ ๊ฒฐํ•ฉ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ assignment๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ํ•  ๋•Œ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ฒด์ด๋‹์ด ์‹œ์ž‘๋œ๋‹ค๋Š” ๋œป
    • false ํ˜น์€ ๋”ฐ๋กœ ์„ค์ •์ด ์—†์„ ๊ฒฝ์šฐ : ํ•ด๋‹น ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ํ• ๋‹น์„ ํ•˜์ง€ ์•Š๋Š” ์—ฐ์‚ฐ์ž์™€ ๊ฐ™์€ ์˜ต์…”๋„ ์ฒด์ด๋‹ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค. ์ฆ‰, ์—ฐ์‚ฐ์ž์— ์˜ต์…”๋„ ์ฒด์ด๋‹ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿšซ ๋งŒ์•ฝ, ์ค‘์œ„ ์—ฐ์‚ฐ์ž๋ฅผ ์ •์˜ํ•  ๋•Œ ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์„ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ DefaultPrecedence๊ทธ๋ฃน์„ ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์œผ๋กœ ๊ฐ–๊ฒŒ๋œ๋‹ค.

 

 

**์„ ์ค‘์œ„ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•ด๋ณด์ž. ์—ฐ์‚ฐ์ž ์ด๋ฆ„์€ **์ด๊ณ , MultiplicationPrecedence ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ ๊ทธ๋ฃน์— ์†ํ•˜๊ฒŒ๋œ๋‹ค.

// String ํƒ€์ž…์˜ contains(_:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Foundation ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ž„ํฌํŠธ
import Foundation

infix operator ** : MultiplicationPrecedence

func ** (lhs: String, rhs: String) -> Bool {
    return lhs.contains(rhs)
}

let helloYejin: String = "hello yejin"
let yejin: String = "yejin"
let isContainsYejin: Bool = helloYejin ** yejin

print(isContainsYejin)	// true

 

 

4๏ธโƒฃ ํด๋ž˜์Šค ๋ฐ ๊ตฌ์กฐ์ฒด์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž ๊ตฌํ˜„

class Car {
    var modelYear: Int?
    var modelName: String?
}

struct SmartPhone {
    var company: String?
    var model: String?
}

// Car ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ผ๋ฆฌ == ์—ฐ์‚ฐํ–ˆ์„ ๋•Œ modelName์ด ๊ฐ™๋‹ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜
func == (lhs: Car, rhs: Car) -> Bool{
    return lhs.modelName == rhs.modelName
}

// SmartPhone ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ๋ผ๋ฆฌ == ์—ฐ์‚ฐํ–ˆ์„ ๋•Œ model์ด ๊ฐ™๋‹ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜
func == (lhs: SmartPhone, rhs: SmartPhone) -> Bool {
    return lhs.model == rhs.model
}

let myCar = Car()
myCar.modelName = "S"

let yourCar = Car()
yourCar.modelName = "S"

var myPhone = SmartPhone()
myPhone.model = "SE"

var yourPhone = SmartPhone()
yourPhone.model = "6"

print(myCar == yourCar)	// true
print(myPhone == yourPhone)	// false

 

ํƒ€์ž…๋งค์„œ๋“œ๋กœ ๊ตฌํ˜„๋œ ์‚ฌ์šฉ์ž ์ •์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž

class Car {
    var modelYear: Int?
    var modelName: String?
    
    // Car ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ผ๋ฆฌ == ์—ฐ์‚ฐ ํ–ˆ์„ ๋•Œ modelName์ด ๊ฐ™๋‹ค๋ฉด true ๋ฐ˜ํ™˜
    static func == (lhs: Car, rhs: Car) -> Bool {
        return lhs.modelName == rhs.modelName
    }
}

struct SmartPhone {
    var company: String?
    var model: String?
    
    static func == (lhs: SmartPhone, rhs: SmartPhone) -> Bool {
        return lhs.model == rhs.model
    }
}

 

 


Reference

Swift: ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์•ผ๊ณฐ