MVC
Model, View, Controller ์ธ ๊ฐ์ ๋ค๋ฅธ ์บ ํ๋ก ๋๋๋ ๊ฒ
Model
โ application์ด ๋ฌด์์ ํ๋๊ฐ. ํ๋ฉด์ ์ด๋ป๊ฒ ๊ทธ๋ ค์ง๋๊ฐ๋ ๊ด๊ณ ์์Controller
โ ๋ชจ๋ธ์ด ์คํฌ๋ฆฐ์ ์ด๋ป๊ฒ ํํ๋ ๊ฒ์ธ๊ฐ. HowView
โ ์ปจํธ๋กค๋ฌ์ ํ์์ข
์์๋ผ ๋ณด๋ฉด ๋จ. ์ปจํธ๋กค๋ฌ๊ฐ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ํ์ํ ๊ฒ (outlet์ด ๊ทธ ์ฐ๊ฒฐ์ ์๋ฏธํจ)
์ด๋ฏธ์ง์์ ๋ ธ๋์ ์ ๊ฑด๋ ์ ์์.
ํฐ ์ ์ ๊ฑด๋ ๋ ์ ์ํด์ผ ํจ.
๋ชจ๋ธ๊ณผ UI๋ ๋ ๋ฆฝ์ ์ด๋ค. ๋ฐ๋ผ์ ์ด ๋์ ์๋ก ์ํตํ ํ์๊ฐ ์๋ค.
๋ทฐ๊ฐ Controller์ ์ด์ผ๊ธฐํ๋ ๊ตฌ์กฐํ๋ ๋ฐฉ์์ target-action์. @IBAction
ScrollView๋ ์ปจํธ๋กค๋ฌ์๊ฒ ์ด๋ป๊ฒ ์คํฌ๋กค์ด ์์๋๋ค๊ณ ๋งํ ๊น?"์ด๋ด, ์ด ์น๊ตฌ ๋ฐฉ๊ธ ์คํฌ๋กค๋ง ์์ํ์ด." "์ฌ์ฉ์๊ฐ ์ค ์ด๋งํผ ํ๋ํ์ด" "์ง๊ธ ์์ง์ผ๋ก ์คํฌ๋กคํ๋ ๊ฑธ ํ์ฉํด๋ ๋ ๊น?"
์ด๋ฐ ์ง๋ฌธ์ด should, will, did ๊ฐ์ ๋ง์ ํฌํจํด์ ์ง๋ฌธ์ ๋์ง๊ฒ ๋๊ณ , ์ด ํ์๋ Delegate
๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ฒ์ ํตํด ์ด๋ฃจ์ด์ง๊ฒ ๋จ.
Delegate
๋ ๋จ์ํ ๋ทฐ ์์ ์๋ ํ๋กํผํฐ, ์ฆ ํ๋กํ ์ฝ์.
ํ๋กํ ์ฝ์ด๋ ๋ค๋ฅธ ๋ ์์ด ๋์ ์คํํ๊ธฐ๋ก ์ฝ์๋ ๋ฉ์๋๋ค์ ๋ชจ์๋์ ์ค๋ช ์.
๋ทฐ๋ ๋น๋ก ๊ทธ ํด๋์ค๊ฐ ๋ฌด์์ธ์ง ๋ชจ๋ฅด๋๋ผ๋ ์ปจํธ๋กค๋ฌ๊ฐ will, should, did๊ฐ์ ๊ฒ์ ์คํํ๊ฒ ๋ค๊ณ ์ฝ์ํ๋ฉด ๋ทฐ๋ ์ปจํธ๋กค๋ฌ์๊ฒ ์ด์ผ๊ธฐํ ์ ์์.
View do not own the data they display.
๋ทฐ๋ ์์ ์ด ๋ณด์ฌ์ฃผ๊ณ ์๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์ ์์.
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ๋ณด์ฌ์ฃผ๋? ์ปจํธ๋กค๋ฌ์๊ฒ ๋ฌผ์ด๋ด์.
์ด ๋๋ will, should, did ๋์ ๋ค๋ฅธ ํ๋กํ ์ฝ์ ๋ฉ์ธ์ง๋ฅผ ๋ฐ์. ์ด ๊ณณ์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌ๋ผ๊ณ ํ๋์ง, ๋ช ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๋์ง.. ์ฌ๊ธฐ ์ฐ์ด๋ delegate๋ฅผ data Source
๋ผ๊ณ ๋ถ๋ฆ.
๋ฐ๋ผ์ ์ด๋ค ๋ทฐ์๋ data Source๋ผ๋ ํ๋กํผํฐ๊ฐ ์๊ณ ์ด ํ๋กํ ์ฝ์ ํฌ์ธํฐ๋ ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ๋จ.
์ปจํธ๋กค๋ฌ๋ ์๊ธฐ์์ ์ ๊ทธ๋ ๊ฒ ์ง์ ํด์ ๋ทฐ๋ฅผ ์ํด ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๋๋ฐ ๊ด์ฌํ๊ฒ ๋จ.
์ฆ, ์ปจํธ๋กค๋ฌ๋ ๋ทฐ์ ๋ชจ๋ธ๊ฐ์ ํด์๊ฐ์.
๋ชจ๋ธ์ด ์ปจํธ๋กค๋ฌ์๊ฒ ์๊ธฐํ ๋ฐฉ๋ฒ์ ์์.
๊ทธ๋ฐ๋ฐ ๋ชจ๋ธ์ด ๊ฐ์ง๊ณ ์๋ UI์ ์๊ด์๋ ๊ฐ์ด, ๋ฐ๋๋ ๋ฐ์ดํฐ๋ผ๋ฉด?
๋ชจ๋ธ์ ๋ณํ๋ฅผ ์๋ ค์ฃผ๊ธฐ ์ํด ๋ผ๋์ค-๋ฐฉ์ก๊ตญ ๋ชจ๋ธ์ ์ด์ฉํจ. ์ปจํธ๋กค๋ฌ๋ ๊ทธ ๋ฐฉ์ก๊ตญ์ ์ฑ๋์ ๋ง์ถ์ด ๋์.
iOS์์๋ ํ๋์ MVC๊ฐ ๋ณดํต ํ๋์ ํ๋ฉด์ ์ ์ดํจ.
์ฌ๋ฌ MVC๋ฅผ ๋ง๋ค์ด์ ๊ฒฐํฉํด์ผ ํ๋์ ํฐ ์ฑ์ด ๋ง๋ค์ด์ง.
๊ธฐ๋ณธ์ ์ผ๋ก MVC๋ ๋ ๋ค๋ฅธ MVC์ ๋ทฐ ์ผ๋ถ๋ก์๋ง ์ผํ ์ ์์.
์์๋ก TabBarController
๊ฐ์ ๋ช๋ช MVC๋ ๋ทฐ์ ์ผ๋ถ๋ก์ 3,4๊ฐ์ ๋ณ๊ฐ์ MVC๋ฅผ ๊ฐ์ง๊ณ ์์.
์ ๋๋ก ๋ชจ๋ธ์ UIKit์ ๋ถ๋ฌ์ค์ง ๋ง์์ผ ํจ.
Foundation โ Core Service ๊ณ์ธต, UI๊ฐ ์๋ ๊ธฐ๋ณธ์ ์ธ ๊ฒ๋ค์ด ์์
API
ํ๋ก๊ทธ๋๋ฐํ ๊ฒ๋ค์ ์ธํฐํ์ด์ค
๊ทธ ์์ ์๋ ๋ฉ์๋์ ํ๋กํผํฐ๊ฐ ์ ๋ถ์.
Button์์ currentTitle์ ์ฝ๊ธฐ์ ์ฉ computed property ์ด๋ฏ๋ก get๋ง ์์.
internal๊ณผ public์ ์ฐจ์ด
internal์ ๋ชจ๋ ์์์๊ณต๊ฐ๋๊ฒ, public์ ๋ค๋ฅธ ๋ชจ๋์๋ ๊ณต๊ฐ๋จ
UIKit์๋ ์๋ง๊ฐ์ public๋ฉ์๋, ์์ฒ๊ฐ์ internal method๊ฐ ์์. ์ด internal ๋ฉ์๋๋ UIKit ํด๋์ค๊ฐ์๋ง ๋ถ๋ฅผ ์ ์์. ์ฐ๋ฆฐ ๊ทธ๊ฒ ๋ญ์ง ์ ์ ์์.
์ฑ์์๋ internal๊ณผ public์ด ๊ฐ๋ค๊ณ ๋ณด๋ฉด ๋จ.
Switch
swift์์ switch๋ ๋ชจ๋ ๊ฐ์ ๊ณ ๋ คํด์ผ ํจ. ๋ง์ฝ String์ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๊ฐ ๋ฌดํ์ด๋ฏ๋ก default๊ฐ ํ์ํจ.
swift์์ switch๋ ๋งค์ฐ ๊ฐ๋ ฅํจ. ์ฐ๊ด๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ ์ํด ํจํด ๋งค์นญ์ ํ ์ ์์.
๋ค์์ ์ฌ์ฉ๋ฒ ์์
func performOperation(symbol: String) {
if let operation = operations[symbol] {
switch operation {
case .Constant(let value):
accumulator = value
case .UnaryOperation(let foo):
accumulator = foo(accumulator)
case .BinaryOperation(let foo):
executePendingOperation()
pending = PendingBinaryOperation(binaryOperation: foo, firstOperand: accumulator)
case .Equals:
executePendingOperation()
}
}
}
Enum
๋ณ๊ฐ์ ๊ฐ์ ๋ชจ์๋์ ์ธํธ, ์๋ก ๊ตฌ๋ณ๋๋ ๊ฐ์ ๊ฐ์ ธ์ผ ํจ
์ค์ํํธ์์ enum์ ํด๋์ค์ฒ๋ผ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์์, ํ์ง๋ง ๋ณ์๋ฅผ ๊ฐ์ง ์ ์์
๊ณ์ฐ ๋ณ์๋ ๊ฐ์ง ์ ์์ง๋ง, ์ ์ฅ ๋ณ์๋ ๊ฐ์ง ์ ์์.
์์ํ๊ฑฐ๋ ๋ฐ์ ์ ์์
value type์.
๋ค์์ ์ฌ์ฉ๋ฒ ์์
var operations:[String:Operation] = [
"e":.Constant(M_E),
"ฯ":.Constant(Double.pi),
"โ":.UnaryOperation(sqrt),
"cos":.UnaryOperation(cos),
"ร":.BinaryOperation({$0 * $1}),
"+":.BinaryOperation({$0 + $1}),
"-":.BinaryOperation({$0 - $1}),
"รท":.BinaryOperation({$0 / $1}),
"=":.Equals
]
enum Operation {
case Constant(Double)
case UnaryOperation((Double) -> Double)
case BinaryOperation((Double, Double) -> Double)
case Equals
}
Struct
value type์
swift์์ ํจ์ํ์ ์ Double Type๊ณผ ์ฐจ์ด๊ฐ ์์. ๋๊ฐ๋ค.
struct๋ class์ ๊ฐ์.
์์์ ํ ์ ์์.
๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์, struct๋ value type์.
class๋ ์ฐธ์กฐ
์ฐธ์กฐํ์์ผ๋ก ์ ๋ฌ๋๋ ๊ฒ์, ๊ทธ๊ฒ heap ์ด๋๊ฐ์ ์๋ค๊ฐ ๋ฉ์๋๋ ์ด๋๋ก ๊ฐ ์ ๋ฌ๋๋ฉด ์ค์ ๋ก pointer(๋ฉ๋ชจ๋ฆฌ)์ฃผ์๋ฅผ ์ ๋ฌํ๋ ๊ฒ์.
๊ฐ์ผ๋ก ์ ๋ฌํ๋ค๋ ๊ฒ์, ์ ๋ฌํ ๋ ๊ทธ๊ฑธ ๋ณต์ฌํ๋ค๋ ๊ฒ์. ๊ทธ๋ ๋ค๊ณ Swift์์ ๊ผญ stack์ ํตํด ๋ณต์ฌ๋๋ ๊ฑด ์๋. Swift๋ ๊ฐ ํ์์ผ๋ก ์ ๋ฌํ ๋, ๊ฑด๋ค๊ธฐ ์ ๊ฐ์ง๋ ์ฑ๋ฅ ํฅ์์ ์ํด ๋ณต์ฌ๊ฐ ์๋จ. (๊ฐ์ ๋ณ๊ฒฝํ ๋ ๋ณต์ฌ๋๋ค๊ณ ํจ)
๊ตฌ์กฐ์ฒด๋ ํด๋์ค์ ๋ค๋ฅด๊ฒ, ๊ตฌ์กฐ์ฒด์์ ์๋ ์์ฑ๋๋ ์ด๊ธฐํ ํจ์๋ ๊ตฌ์กฐ์ฒด ์์ ์๋ ๋ชจ๋ ๋ณ์๋ฅผ ์ ๋ ฅ์ธ์๋ก ๊ฐ๊ฒ ๋จ.
๋ค์์ ์ฌ์ฉ๋ฒ ์์์.
struct PendingBinaryOperation {
var binaryOperation: (Double, Double) -> Double
var firstOperand: Double
}
Closure
๊ธฐ๋ณธ์ ์ผ๋ก ์ธ๋ผ์ธ ํจ์ ( ์ธ๋ผ์ธ ํจ์๋ ํจ์ํธ์ถ์์ด ๊ทธ ์์น์์ ๋ฐ๋ก ์คํ๋๋ ํจ์)
ํ๊ฒฝ ์ํ๋ฅผ ์บก์ณํ๋ ์ธ๋ผ์ธ ํจ์
'iOS ๐ฅ > Stanford Lecture' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Stanford iOS] Lecture 1) ์ ๋ฆฌ (Cocoa Touch, MVC, Optional) (0) | 2021.01.24 |
---|