본문 바로가기
공부/iOS

iOS 프로그래밍 5주차

by Electrohyun 2025. 9. 30.
  • 함수 2
  • 1급 객체(first class object) 
  • 클로저(closure)
  • 고차 함수(higher-order function)
  • 클래스 기초

복습

4가지 Swift 함수 실습 (출처: Smile Han의 iOS 프로그래밍)

- 콜론 주의하기

 

print() 함수 사용법 (출처: Smile Han의 iOS 프로그래밍)
실습결과

 

sss 함수의 자료형?

 

 

import Foundation이 포인트
신기한 가변 매개변수(Variadic Parameter)

 

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

 

Swift에서의 call by address - inout (출처: Smile Han의 iOS 프로그래밍)

 


- 일급 객체

 

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

 


- 클래스

Swift에서 클래스 선언하기 (출처: Smile Han의 iOS 프로그래밍)

 

에러가 발생하는 이유? (var age = Int는 오타)

 

스위프트에서 클래스를 만들 때는(stored property의 경우) 초기값이 없을 시 에러 발생함.

- 초기값을 주거나

- 옵셔널로 지정하거나

- 생성자를 작성해야함.

 

- 생성자 함수는 func 붙이지 않음

- swift에서 initializer, constructor를 (일단) 같다고 표현하는 건가? 싶었다.

 

= Man()을 붙이지 않으면 에러가 발생한다.

- 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