[Swift] ν¨μ : μ μΆλ ₯ λ§€κ°λ³μ νμ©, λ°μ΄ν° νμ μΌλ‘μ¨μ ν¨μ, μ€μ²© ν¨μ, @discardableResult
π« 곡λΆνλ€κ° κΈ°λ‘ν΄λλ©΄ μ’κ² λ€, λͺ°λλ κ±°λ€ μΆμ λ΄μ©μ μμ£Όλ‘ μ 리νμ΅λλ€.
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 μ€μννΈ νλ‘κ·Έλλ° - μΌκ³°