본문 바로가기

iOS

iOS_3grade - 4주차

argument label 과 parameter name 을 구분하지 않고 하나만 쓸 경우에는 호출할 때와 함수 내부에서 동일한 이름으로 변수가 쓰임

Swift에서 디폴트 인자(default parameter) 를 쓰는 이유


1️⃣ 함수 호출 편리하게

  • 매번 같은 값을 전달할 필요 없음
func greet(name: String = "Guest") {
    print("Hello, \(name)!")
}

greet()          // Hello, Guest!
greet(name: "Tom") // Hello, Tom!
  • 호출할 때 인자를 생략 가능 → 코드 간결

2️⃣ 오버로딩(overload) 줄이기

  • 같은 기능의 여러 함수를 만들 필요 없음
func drawCircle(radius: Double, color: String = "Red") { ... }
  • 기존 방식이면
func drawCircle(radius: Double) { ... }
func drawCircle(radius: Double, color: String) { ... }
  • 디폴트 인자로 하나로 통합 가능

3️⃣ 가독성 향상

  • 함수 선언만 보고도 어떤 값이 기본으로 쓰이는지 바로 이해 가능
  • 호출 시 불필요한 값 생략 → 코드 읽기 쉬움

 

일급객체

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)")
}
upDown(Fun:toUp, value: 10) //toUp(10)
upDown(Fun:toDown, value: 10) //toDown(10)


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)")
}
upDown(Fun:toUp, value: 10) //toUp(10)
upDown(Fun:toDown, value: 10) //toDown(10)

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)

 

클로저

클로저 multiply의 자료형은 함수의 자료형과 같음

- 후행 클로저

func mul(a: Int, b: Int) -> Int {
    return a * b
}
let multiply = {(a: Int, b: Int) -> Int in
    return a * b
}
print(mul(a:10, b:20))
print(multiply(10, 20))
let add = {(a: Int, b: Int) -> Int in
    return a + b
}
print(add(10, 20))

func math(x: Int, y: Int, cal: (Int, Int) -> Int) -> Int {
    return cal(x, y)
}
var result = math(x: 10, y: 20, cal: add) //return add(10,20)
print(result)

math(x: 3, y: 5, cal: multiply)
result = math(x: 2, y: 3, cal: {(a: Int, b: Int) -> Int in
    return a + b
}) // 클로저 (매개변수로 사용, 함수이기 때문)
print(result)

result = math(x: 2, y: 3){(a: Int, b: Int) -> Int in
    return a + b
} // 후행 클로저

아규먼트 레이블 생략 / -> Int (리턴형) 생략 / trailing closure 후행 클로저, 함수가 매개변수 마지막일때 밖으로 빼냄 / 매개변수 생략 (a: Int, b: Int) in 후 단축인자 사용 a + b > $0 + $1 / 리턴 생략 return $0 + $1 > $0 + $1

 ㄴ 최종 : 함수 $0 , $1로 생략

 

저장 프로퍼티 (stored property)

반드시 초기값이 있어야 한다.

프로퍼티는

1. 초기값이 있거나

2. init을 이용해서 초기화하거나

3. 옵셔널 변수(상수)로 선언 (자동으로 nil로 초기화)

 

computed propert는 값을 가지고 있는게 아닌 필요할 때 계산해서 만들어 줌

 

상속 가능한 부모 클래스는 하나이고, 나머지는 프로토콜 (추상 클래스와 비슷한 개념)

가지고 있는 프로퍼티를 모두 초기화 시키는 생성자

 

접근 제어자

많은 access control이 있지만 거의 internal만 쓴다고 생각해도됨

'iOS' 카테고리의 다른 글

iOS_3grade - 6주차  (1) 2026.04.07
iOS_grade3 - 5주차  (0) 2026.03.31
iOS_3grade - 3주차  (1) 2026.03.17
iOS_3grade - 2주차  (0) 2026.03.10
iOS_3grade - 1주차  (1) 2026.03.03