Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift BASIC

[Swift] ๊ธฐ์ดˆ ๋ฌธ๋ฒ• - ํด๋กœ์ €์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ๊ฐœ๋… (closure)

๊ฐ์ž ๐Ÿฅ” 2022. 1. 23. 13:18
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์ž์ž…๋‹ˆ๋‹ค! ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ํด๋กœ์ €์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค.

๋ณธ ํฌ์ŠคํŒ…์€ ํŒจ์ŠคํŠธ์บ ํ”„ 30๊ฐœ ํ”„๋กœ์ ํŠธ๋กœ ๋ฐฐ์šฐ๋Š” ios์•ฑ ๊ฐœ๋ฐœ, ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋“ค์„ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ถœ์ฒ˜๋Š” ๋ฐ‘์—๋‚จ๊ฒจ์š”!


ํด๋กœ์ €๋ž€?

ํด๋กœ์ €๋Š” ์ฐธ์กฐํƒ€์ž…์ด๊ณ , ์ฝ”๋“œ์—์„œ ์ „๋‹ฌ ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋…๋ฆฝ๊ธฐ๋Šฅ์ด๋ฉฐ, ์ผ๊ธ‰ ๊ฐ์ฒด์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ์ผ๊ธ‰๊ฐ์ฒด๋ž€ ์ „๋‹ฌ์ธ์ž๋กœ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๊ณ , ๋ณ€์ˆ˜/์ƒ์ˆ˜ ๋“ฑ์œผ๋กœ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ๋  ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ๋ณดํ†ต ํด๋กœ์ €๋ผ๊ณ  ํ•˜๋ฉด ์ด๋ฆ„์—†๋Š” ํ•จ์ˆ˜ ์ฆ‰, ์ต๋ช…ํ•จ์ˆ˜๋ฅผ ์ง€์นญํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ํด๋กœ์ €๋Š” named ํด๋กœ์ €, unnamed ํด๋กœ์ € ๋‘˜๋‹ค ํฌํ•จํ•œ๋‹ค.

ํด๋กœ์ € ํ‘œํ˜„์‹์€ ๋‹ค์Œ๊ณผ๊ฐ™๋‹ค. ์ต๋ช…ํ•จ์ˆ˜์—ฌ์„œ func๋ผ๋Š” ํ‚ค์›Œ๋“œ๋Š” ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํด๋กœ์ € ํ—ค๋“œ์™€ ํด๋กœ์ € ๋ฐ”๋””๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค. 

๋…ธ๋ž€์ƒ‰ ๋ถ€๋ถ„์ด ํด๋กœ์ € ํ—ค๋“œ, ํ•‘ํฌ์ƒ‰ ๋ถ€๋ถ„์ด ํด๋กœ์ € ๋ฐ”๋””์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘˜์„ ๊ตฌ๋ถ„ ์ง€์–ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ in์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํด๋กœ์ €์˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

ํด๋กœ์ €์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

ํด๋กœ์ €๋Š” ์ต๋ช…์ด๊ธด ํ•˜์ง€๋งŒ ํ•จ์ˆ˜์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ผ๊ธ‰๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜์— ํด๋กœ์ €๋ฅผ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

1) ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ด๊ฐ’์ด ๋ชจ๋‘ ์—†๋Š” ํ˜•ํƒœ์˜ ํด๋กœ์ €๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž.

let hello = { () -> () in
	print("hello")
}

hello()
//prints "hello"

2) ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ดํƒ€์ž…์ด ์žˆ๋Š” ํด๋กœ์ €๋„ ๋งŒ๋“ค์–ด๋ณด์ž.

let hello2 = { (name: String) -> String in
	return "Hello, \(name)"
}

 

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด๋ณด์ž.

hello2(name: "potato")

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์™œ ? ํ•จ์ˆ˜์ด๊ธด ํ•œ๋‹ค๋ฉด์„œ ์™œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์ผ๊นŒ?
์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์ด ์žˆ๋‹ค. ํ•จ์ˆ˜์—์„œ ๋ฐฐ์šด๋Œ€๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ name์€ ๋‹จ๋…์œผ๋กœ ์“ฐ์˜€์œผ๋‹ˆ, ์ด name์ด ์ „๋‹ฌ์ธ์ž์˜ ๋ ˆ์ด๋ธ”์ด์ž ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์œผ๋กœ name์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ตฌ๋‚˜~ ํ•˜๊ณ  ์ดํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ํด๋กœ์ €์—์„œ๋Š” ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”์ด๋ผ๋Š” ๊ฐœ๋…์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์œ„ ์ฝ”๋“œ์—์„œ ํ˜„์žฌ name์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ๋งŒ ์“ฐ์ด๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ํ•จ์ˆ˜(์ „๋‹ฌ์ธ์ž: "!@#") ๋ผ๊ณ  ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์ฒ˜๋Ÿผ ํ˜ธ์ถœํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—๋Ÿฌ ์—†์ด ํด๋กœ์ €๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์จ์•ผํ•œ๋‹ค.

hello2("potato")
// prints Hello, potato

์ „๋‹ฌ์ธ์ž ์ด๋ฆ„์„ ์ ์ง€ ์•Š๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’๋งŒ ๋ฐ”๋กœ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿผ ํด๋กœ์ €์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.


3) ๊ทธ๋ฆฌ๊ณ  ํด๋กœ์ €๋Š”, ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์œผ๋กœ ํด๋กœ์ €๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

ํด๋กœ์ €๋ฅผ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

func doSomething(closure: () -> ()) {
	closure()
}

doSomethingํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํด๋กœ์ €๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ์ž‘์„ฑํ–ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ closure๋ผ๊ณ  ํ–ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ดํƒ€์ž…์ด ์—†๋Š” ํด๋กœ์ €๋ฅผ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  doSomethingํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด closure()๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

์ด์ œ doSomething์„ ํ˜ธ์ถœํ•ด๋ณด์ž.

doSomething(closure: { () -> () in
	print("hello")
})

closure์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— closureํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ๋ณด์ž. ๋ฐ˜ํ™˜๊ฐ’๊ณผ ๋ฆฌํ„ด์ด ์—†๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ () -> ()๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๊ณ , in์‹คํ–‰๊ตฌ๋ฌธ์œผ๋กœ printํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ํ•˜๋ฉด "hello"๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 


4) ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์— ํด๋กœ์ €๋ฅผ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

func doSomething2() -> () -> () {
	return { () -> () in
    	print("hellohellohello")
	}
}

doSomething2()() //ํ˜ธ์ถœ
//prints "hellohellohello"

์ด์ฒ˜๋Ÿผ closure๋Š” ์ผ๊ธ‰๊ฐ์ฒด์˜ ํŠน์ง•์„ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ  ์žˆ์–ด์„œ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›๊ณ , ๋ฆฌํ„ดํƒ€์ž…์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

ํด๋กœ์ €๊ฐ€ ์กฐ๊ธˆ ๊ธธ์–ด์ง€๊ฑฐ๋‚˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋ฉด ํ›„ํ–‰ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ์จ ๊ฐ€๋…์„ฑ ๋†’์€ ์ฝ”๋“œ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  1. ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํด๋กœ์ €์˜ ํƒ€์ž…์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋‚˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ƒ๋žตํ•ด์„œ ์ž‘์„ฑํ• ์ˆ˜ ์žˆ๋‹ค.
  2. ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์•”๋ฌต์ ์œผ๋กœ ํด๋กœ์ €์˜ ๋งจ ๋งˆ์ง€๋ง‰์ค„์€ returnํ‚ค์›Œ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋”๋ผ๋„ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
  3. ์ „๋‹ฌ์ธ์ž์˜ ์ด๋ฆ„์ด ๊ตณ์ด ํ•„์š”์—†๊ณ , ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž… ์œ ์ถ”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ถ•์•ฝ๋œ ์ „๋‹ฌ์ธ์ž ์ด๋ฆ„ ($0, $1, $2....)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด ๋ชจ๋“  ๊ฒƒ์„ ํ•œ ํฌ์ŠคํŒ…์œผ๋กœ ์—…๋กœ๋“œํ•˜๋Š”๊ฒƒ์€ ์–ด๋ ต๊ธฐ๋„ํ•˜๊ณ  ํด๋กœ์ €์˜ ๊ฐœ๋…์€ ๋„“๊ณ ๋„ ๋„“๊ธฐ ๋•Œ๋ฌธ์— ์ถ”ํ›„ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ ๊ด€๋ จ๋œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋ฉด ๋” ์ถ”๊ฐ€์ ์œผ๋กœ ๊ณต๋ถ€ํ•ด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ํ•˜๊ณ , ์ง€๊ธˆ์€ ์–ด๋–ค์‹์œผ๋กœ ์ถ•์•ฝ์ด ์ด๋ฃจ์–ด์ ธ์„œ ์ฝ”๋“œ๊ฐ€ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง€๋Š”์ง€ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

func doSomething(closure: (Int, Int, Int) -> Int) {
	closure(1,2,3)
}

// doSomethingํ˜ธ์ถœ

//์ถ•์•ฝ์ „
doSomething(closure: { (a, b, c) in
	return a+b+c
})

//๋‹จ๊ณ„๋ณ„ ์ถ•์•ฝ ํ›„
doSomething(closure: {
	return $0+$1+$2
})

doSomething(closure: {
	$0+$1+$2
})

doSomething() {
	$0+$1+$2
}


//์ตœ์ข…์ ์œผ๋กœ ์ถ•์•ฝํ•œ ํ˜•ํƒœ
doSomething {
	$0+$1+$2
}

 

 

์ถ”ํ›„ ๋” ๊ณต๋ถ€ํ•ด์„œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.  

๋ฐ˜์‘ํ˜•