Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

브래의 슬기로운 코딩 생활

iOS프로그래밍 실무 11주차 정리 본문

2-1/iOS프로그래밍 실무

iOS프로그래밍 실무 11주차 정리

김브래 2023. 5. 17. 17:10

delegation design pattern

 

- class(struct)가 책임의 일부를 다른 유형의 인스턴스에 전달(또는 위임)할 수 있도록 하는 디자인 패턴

- protocol에 위임된 목록을 작성하고, delegate(보통 내가 만든 class)는 위임된 기능을 작성

 

프로토콜은 다중 상속이 가능함.

 

optional: 필요한 경우 구현하는 메서드

required: 필수로 구현해야하는 메서드

 

~.delegate = self

~.datasource = self

 

delegate 소스에는 모든 메소드는 optional이다.

 

열거형(enum)

.~ : .앞에 열거형 생략

 

enum Week: String {

case Mon,Tue,Wed,Thur,Fri,Sat,Sun

func printWeek() { //메서드도 가능

switch self {

case .Mon, .Tue, .Wed, .Thur, .Fri:

print("주중")

case .Sat, .Sun:

print("주말")

}

}

}

Week.Sun.printWeek() //주말

 

enum Date {

case intDate(Int,Int,Int) //(int,Int,Int)형 연관값을 갖는 intDate

case stringDate(String) //String형 연관값을 값는 stringDate

}

var todayDate = Date.intDate(2023,4,30)

todayDate = Date.stringDate("2023520") //주석처리하면?

switch todayDate {

case .intDate(let year, let month, let day):

print("\(year)\(month)\(day)")

case .stringDate(let date):

print(date)//2023520

}

 

구조체

 

memberwise initializer가 자동으로 만들어짐

Int, Double, String 등 기본 자료형은 구조체

Array, Dictionary, SetGeneric Structure

nil도 구조체

구조체/enum의 인스턴스는 값(value) 타입, 클래스의 인스턴스는 참조(reference) 타입

구조체는 상속 불가

 

memberwise initializer : 구조체가 자동으로 만들어주는 init() 메소드 //구조체 사용의 장점

Swift의 기본 데이터 타입은 모두 구조체

 

클래스(class)와 구조체(structure)의 공통점

프로퍼티(property)를 정의할 수 있음

메서드를 정의할 수 있음

[]를 사용해 첨자(subscript) 문법으로 내부의 값을 액세스할 수 있는 첨자를 정의할 수 있음

클래스, 구조체, 열거형이 컬렉션 멤버에 접근하기 위한 문법

초기 상태 설정을 위한 초기화 함수(initializer)를 정의할 수 있음

extension 가능

protocol 채택 가능

 

classstruct보다 더 갖는 특징

상속이 가능

타입 캐스팅(is as as? as!)을 통해 실행 시점에 클래스 인스턴스의 타입을 검사하고 해석 가능

deinitializer(deinit{})로 사용한 자원을 반환 가능

참조 카운팅을 통해 한 클래스 인스턴스를 여러 곳에서 참조(사용) 가능

 

 

Generic <>: <>는 결정되지 않은 자료형으로 범용 타입으로 사용가능

 

mutating 키워드를 사용하면 프로퍼티의 값 변경 가능

 

func myPrint<T>(a: T, b: T) {

print(b,a)

}

myPrint(a:1,b:2)

myPrint(a:2.5,b:3.5)

myPrint(a:"Hi",b:"Hello")

// 2 1

// 3.5 2.5

// Hello Hi

 

struct IntStack <T> {

var items = [T]()

mutating func push(_ item: T) {

items.append(item)

}

mutating func pop() -> T {

return items.removeLast()

}

}

var stackOfInt = IntStack<Double>()

print(stackOfInt.items)

stackOfInt.push(1.2)

print(stackOfInt.items)

stackOfInt.push(2.5)

print(stackOfInt.items)

stackOfInt.push(5.8)

print(stackOfInt.items)

print(stackOfInt.pop())

print(stackOfInt.items)

print(stackOfInt.pop())

print(stackOfInt.items)

print(stackOfInt.pop())

print(stackOfInt.items)

 

과제 : 소스를 class 하나와 2개의 extension으로 나누기

 

ViewController.swift

class ViewController: UIViewController{

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {

super.viewDidLoad()

table.dataSource = self

table.delegate = self

}

extension ViewController : UITableViewDelegate{

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

print(indexPath.description)

}

}

extension ViewController : UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {

return 1

}

 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 10

}

 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyTableViewCell

cell.movieName.text = movieData?.boxOfficeResult.dailyBoxOfficeList[indexPath.row].movieNm

return cell

}

}