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 ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์•ผ๊ณฐ