2일 차

Dzeko·2021년 6월 30일

Swift 기본

목록 보기
3/20
post-thumbnail

2일차

델리게이트 패턴

델리게이트 패턴은 쉽게 말해, 객체 지향 프로그래밍에서 하나의 객체가 모든 일을 처리하는 것이 아니라 처리 해야 할 일 중 일부를 다른 객체에 넘기는 것을 뜻한다.

상속

클래스는 부모 클래스와 자식 클래스로 구분할 수 있다. 클래스를 상속받는다는 것은 상속받고자 하는 클래스의 변수 및 함수를 모두 사용할 수 있다는 것.
상속을 받기 위해서는 클래스를 선언하면서 클래스의 이름 오른쪽에 ‘ : ’ 와 함께 상속받을 클래스의 이름을 입력하면 됨.
‘ViewController.swift’에서 ViewController 클래스를 선언할 때는 자동으로 UIViewController 클래스를 상속받음

class ViewController: UIViewController {
	…
}

다른 클래스를 상속받으려면 UIViewController 뒤에 추가하면 됨

class ViewController: UIViewController,  UIPickerViewDelegate, UIPickerViewDataSource{
	…
}

배열

  1. 변수 선언과 동시에 값을 입력
    var name: [String] = [“김구”, ”김두한”, ”김종국”]
    var score: [Int] = [3, 5, 7]
  2. 빈 배열을 선언하고 값을 추가
    var name: String
    name.append(“김구”)

함수

https://jusung.gitbook.io/the-swift-language-guide/language-guide/06-functions

함수 인자 라벨과 파라미터 이름 (Function Argument Labels and Parameter Names)

함수 호출시 적절한 파라미터 이름을 지정해 함수 내부와 함수 호출시 사용할 수 있습니다.

func someFunction(firstParameterName: Int, secondParameterName: Int) {
// 함수 내부에서 firstParameterName와 secondParameterName의 인자를 사용합니다.
}
someFunction(firstParameterName: 1, secondParameterName: 2)

인자 라벨 지정 (Specifying Argument Labels)

파라미터 앞에 인자 라벨을 지정해 실제 함수 내부에서 해당 인자를 식별하기 위한 이름과 함수 호출시 사용하는 이름을 다르게 해서 사용할 수 있습니다.

func someFunction(argumentLabel parameterName: Int) {
    // 함수 안애서 parameterName로 argumentLabel의 인자값을 참조할 수 있습니다.
}

인자 라벨을 지정해서 함수 내부에서는 hometown으로 값을 제어하고 함수 호출시에는 인자 값으로 from을 사용한 (예) 입니다.

func greet(person: String, from hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill!  Glad you could visit from Cupertino."

인자 생략 (Omitting Argument Labels)

파라미터 앞에 _를 붙여 함수 호출시 인자값을 생략할 수 있습니다.

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    // 함수 안에서 firstParameterName, secondParameterName
    // 인자로 입력받은 첫번째, 두번째 값을 참조합니다.
}
someFunction(1, secondParameterName: 2)

아직까지는 감이 잘 안온다.

0개의 댓글