- 함수 2
- 1급 객체(first class object)
- 클로저(closure)
- 고차 함수(higher-order function)
- 클래스 기초
복습


- 콜론 주의하기






- 자바스크립트 스프레드 연산자 같아서 신기함

- 일급 객체

func up(num: Int) -> Int {
return num + 1
}
func down(num: Int) -> Int {
return num - 1
}
let toUp = up
print(up(num:10))
print(toUp(10))
let toDown = down
func upDown(Fun: (Int) -> Int, value: Int) {
let result = Fun(value)
print("결과= \(result)")
}
print(type(of: upDown))
upDown(Fun: toUp, value: 10)
upDown(Fun: toDown, value: 10)
upDown(Fun: toUp, value: 20)
func decideFun(x: Bool) -> (Int) -> Int {
if x {
return toUp
} else {
return toDown
}
}
let r = decideFun(x:true) // let r = toUp
print(type(of:r)) //(Int) -> Int
print(r(10)) // toUp(10)
- 실행결과
11
11
@Sendable ((Int) -> Int, Int) -> ()
결과= 11
결과= 9
결과= 21
(Int) -> Int
11
- 클래스


스위프트에서 클래스를 만들 때는(stored property의 경우) 초기값이 없을 시 에러 발생함.
- 초기값을 주거나
- 옵셔널로 지정하거나
- 생성자를 작성해야함.

- 생성자 함수는 func 붙이지 않음
- swift에서 initializer, constructor를 (일단) 같다고 표현하는 건가? 싶었다.

- Init()은 생략 가능!
'공부 > iOS' 카테고리의 다른 글
| iOS 프로그래밍 9주차 (0) | 2025.10.28 |
|---|---|
| iOS 프로그래밍 7주차 (0) | 2025.10.14 |
| iOS 프로그래밍 4주차 (0) | 2025.09.23 |
| iOS 프로그래밍 3주차 (0) | 2025.09.16 |
| iOS 프로그래밍 2주차 (0) | 2025.09.09 |