Swift

[Swift] ν•¨μˆ˜ : μž…μΆœλ ₯ λ§€κ°œλ³€μˆ˜ ν™œμš©, 데이터 νƒ€μž…μœΌλ‘œμ¨μ˜ ν•¨μˆ˜, 쀑첩 ν•¨μˆ˜, @discardableResult

yevdev 2022. 3. 16. 18:23

🚫 κ³΅λΆ€ν•˜λ‹€κ°€ 기둝해두면 μ’‹κ² λ‹€, λͺ°λžλ˜ κ±°λ‹€ 싢은 λ‚΄μš©μ„ μœ„μ£Όλ‘œ μ •λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.

 

 

1️⃣ μž…μΆœλ ₯ λ§€κ°œλ³€μˆ˜λ₯Ό ν™œμš©

값이 μ•„λ‹Œ μ°Έμ‘°λ₯Ό μ „λ‹¬ν•˜λ €λ©΄ μž…μΆœλ ₯ λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•œλ‹€. μ›λž˜ 값을 λ³€κ²½ν•œλ‹€λŠ” 이야기!

1. ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ–„, μ „λ‹¬μΈμžμ˜ 값을 λ³΅μ‚¬ν•©λ‹ˆλ‹€.

2. ν•΄λ‹Ή μ „λ‹¬μΈμžμ˜ 값을 λ³€κ²½ν•˜λ©΄ 1μ—μ„œ λ³΅μ‚¬ν•œ 것을 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ λ³€κ²½ν•©λ‹ˆλ‹€.

3. ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•˜λŠ” μ‹œμ μ— 2μ—μ„œ λ³€κ²½λœ 값을 μ›λž˜μ˜ λ§€κ°œλ³€μˆ˜μ— ν• λ‹Ήν•©λ‹ˆλ‹€.

var numbers: [Int] = [1,2,3]

func nonReferenceParameter(_ arr: [Int]){
    var copiedArr: [Int] = arr
    copiedArr[1] = 1
}

func referenceParameter(_ arr: inout[Int]){
    arr[1] = 1
}

nonReferenceParameter(numbers)
print(numbers[1])

referenceParameter(&numbers)	// μ°Έμ‘°λ₯Ό ν‘œν˜„ν•˜κΈ° μœ„ν•΄ &을 λΆ™μ—¬μ€λ‹ˆλ‹€.
print(numbers[1])

// 2
// 1

μž…μΆœλ ₯ 맀개 λ³€μˆ˜λŠ” λ§€κ°œλ³€μˆ˜ 기본값을 κ°€μ§ˆ 수 μ—†μœΌλ©°, κ°€λ³€ λ§€κ°œλ³€μˆ˜λ‘œ μ‚¬μš©λ  μˆ˜λ„ μ—†λ‹€.

λ˜ν•œ, μƒμˆ˜λŠ” 변경될 수 μ—†μœΌλ―€λ‘œ μž…μΆœλ ₯ λ§€κ°œλ³€μˆ˜μ˜ μ „λ‹¬μΈμžλ‘œ μ‚¬μš©λ  수 μ—†λ‹€.

🚫 μž…μΆœλ ₯ λ§€κ°œλ³€μˆ˜λŠ” 잘 μ‚¬μš©ν•˜λ©΄ 문제 μ—†μ§€λ§Œ 잘λͺ» μ‚¬μš©ν•˜λ©΄ λ©”λͺ¨λ¦¬ μ•ˆμ „μ„ μœ„ν˜‘ν•˜μ—¬ μ‚¬μš©μ— λͺ‡λͺ‡ μ œμ•½μ΄ μžˆλ‹€.

 

 

2οΈβƒ£λ°μ΄ν„°νƒ€μž…μœΌλ‘œμ„œμ˜ ν•¨μˆ˜

μŠ€μœ„ν”„νŠΈμ˜ ν•¨μˆ˜λŠ” 일급 객체 μ΄λ―€λ‘œ ν•˜λ‚˜μ˜ 데이터 νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

즉, 각 ν•¨μˆ˜λŠ” λ§€κ°œλ³€μˆ˜ νƒ€μž…κ³Ό λ°˜ν™˜ νƒ€μž…μœΌλ‘œ κ΅¬μ„±λœ ν•˜λ‚˜μ˜ νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€λŠ” 뜻

μ˜ˆμ‹œ

typealias CalculateTwoInts = (Int, Int) -> Int

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a+b
}

func multiplyTwoInts(_ a: Int, _ b: Int) -> Int{
    return a*b
}

var mathFunction: CalculateTwoInts = addTwoInts
// var mathFunction: (Int, Int) -> Int = addTwoInts와 λ™μΌν•œ ν‘œν˜„μ΄λ‹€

print(mathFunction(2,5))

mathFunction = multiplyTwoInts
print(mathFunction(2,5))

// 7
// 10

μ•„λž˜μ˜ μ½”λ“œμ²˜λŸΌ μ „λ‹¬μΈμžλ‘œ ν•¨μˆ˜λ₯Ό λ„˜κ²¨ 쀄 μˆ˜λ„ μžˆλ‹€.

typealias CalculateTwoInts = (Int, Int) -> Int

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a+b
}

func multiplyTwoInts(_ a: Int, _ b: Int) -> Int{
    return a*b
}

func printMathResult(_ mathFunction: CalculateTwoInts, _ a: Int, _ b: Int){
    print("Result: \(mathFunction(a,b))")
}

printMathResult(addTwoInts, 3, 5)

// Result: 8

νŠΉμ • 쑰건에 따라 μ μ ˆν•œ ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•΄μ€„ μˆ˜λ„ μžˆλ‹€.

typealias CalculateTwoInts = (Int, Int) -> Int

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a+b
}

func multiplyTwoInts(_ a: Int, _ b: Int) -> Int{
    return a*b
}

func printMathResult(_ mathFunction: CalculateTwoInts, _ a: Int, _ b: Int){
    print("Result: \(mathFunction(a,b))")
}

func chooseMathFunction(_ toAdd: Bool) -> CalculateTwoInts{
    return toAdd ? addTwoInts : multiplyTwoInts
}

printMathResult(chooseMathFunction(true), 3, 5)

// Result: 8

 

 

3️⃣ μ€‘μ²©ν•¨μˆ˜ μ‚¬μš©ν•˜κΈ°

μ›μ μœΌλ‘œ μ΄λ™ν•˜κΈ° μœ„ν•œ ν•¨μˆ˜ (μ€‘μ²©ν•¨μˆ˜ μ‚¬μš© X)

typealias MoveFunc = (Int) -> (Int)

func goRight(_ currentPosition: Int) -> Int {
    return currentPosition + 1
}

func goLeft(_ currentPosition: Int) -> Int {
    return currentPosition - 1
}

func functionForMove(_ shouldGoLeft: Bool) -> MoveFunc {
    return shouldGoLeft ? goLeft : goRight
}

var position: Int = 3

let moveToZero: MoveFunc = functionForMove(position > 0)
print("μ›μ μœΌλ‘œ κ°‘μ‹œλ‹€")

while position != 0 {
    print("\(position)...")
    position = moveToZero(position)
}

print("원점 도착")

// μ›μ μœΌλ‘œ κ°‘μ‹œλ‹€
// 3...
// 2...
// 1...
// 원점 도착

μ€‘μ²©ν•¨μˆ˜ μ‚¬μš©

typealias MoveFunc = (Int) -> (Int)

func functionForMove(_ shouldGoLeft: Bool) -> MoveFunc {
    
    func goRight(_ currentPosition: Int) -> Int {
        return currentPosition + 1
    }

    func goLeft(_ currentPosition: Int) -> Int {
        return currentPosition - 1
    }

    return shouldGoLeft ? goLeft : goRight
}

var position: Int = -4

let moveToZero: MoveFunc = functionForMove(position > 0)
print("μ›μ μœΌλ‘œ κ°‘μ‹œλ‹€")

while position != 0 {
    print("\(position)...")
    position = moveToZero(position)
}

print("원점 도착")

// μ›μ μœΌλ‘œ κ°‘μ‹œλ‹€
// -4...
// -3...
// -2...
// -1...
// 원점 도착

 

 

4️⃣ λ°˜ν™˜ 값을 λ¬΄μ‹œν•  수 μžˆλŠ” ν•¨μˆ˜

@discardableResult μ„ μ–Έ 속성 μ‚¬μš©

@discardableResult ? λ°˜ν™˜ 값을 μ‚¬μš©ν•˜μ§€ μ•Šμ•˜λ‹€κ³  미리 μ•Œλ¦ΌμœΌλ‘œμ¨, μ»΄νŒŒμΌλŸ¬κ°€ 이에 λŒ€ν•œ κ²½κ³  문ꡬλ₯Ό 날리지 μ•Šλ„λ‘ ν•œλ‹€.

func say(_ something: String) -> String{
    print(something)
    return something
}

@discardableResult func discardableResultSay(_ something: String)-> String{
    print(something)
    return something
}

// λ°˜ν™˜ 값을 μ‚¬μš©ν•˜μ§€ μ•Šμ•˜μœΌλ―€λ‘œ μ»΄νŒŒμΌλŸ¬κ°€ κ²½κ³ λ₯Ό ν‘œμ‹œν•  수 μžˆλ‹€.
say("Hello")

// λ°˜ν™˜ 값을 μ‚¬μš©ν•˜μ§€ μ•Šμ„ 수 μžˆλ‹€κ³  미리 μ•Œλ ΈκΈ° λ•Œλ¬Έμ—
// λ°˜ν™˜ 값을 μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ μ»΄νŒŒμΌλŸ¬κ°€ κ²½κ³ ν•˜μ§€ μ•ŠλŠ”λ‹€.
discardableResultSay("Hello")

// Hello
// Hello

 

 


Reference

Swift μŠ€μœ„ν”„νŠΈ ν”„λ‘œκ·Έλž˜λ° - μ•Όκ³°