INDEX
프로토콜 지향 언어
ArrayList
DataSource
Delegate
프로토콜 지향 언어
스위프트는 프로토콜 지향 언어이다. (Protocol-Oriented language)
스위프트는 대부분 구조체로 이어져있고, 구조체는 상속이 불가능하다는 특징을 가진다.
상속도 되지 않는 구조체를 가지고 다양한 공통기능을 가질 수 있던 이유는 뭘까?
바로 Protocol과 Extension에 있다.
프로토콜 지향 프로그래밍이란?
필요한 부분만을 프로토콜로 분리하여 프로그래밍하는 것
예제) 프로토콜 사용법
1. Talkable 이라는 프로토콜을 정의한다.
protocol Talkable {
var topic: String { get set }
func talk(to: Self)
}
2. Talkable 이라는 프로토콜을 채택할 구조체를 정의한다.
struct Person: Talkable {
var topic: String
var name: String
func talk(to: Person) {
// talk 구현
}
}
3. 프로토콜의 메소드 호출
let elly = Person(topic: "Swift", name: "elly")
let coco = Person(topic: "Java", name: "Coco")
elly.talk(to: coco)
coco.talk(to: elly)
그런데 Talkable 프로토콜을 채택한 구조체가 많아질수록 매번 talk(to: )를 구현해야하는 번거로움이 있다.
이 때 사용하는 것이 **Extension**이다.
프로토콜 초기구현
// 익스텐션을 사용한 프로토콜 초기 구현
extension Talkable {
func talk(to: Self) {
print("\\(to)! \\(topic)")
}
}
이렇게 하나의 프로토콜을 만들고, 초기 구현을 해둔다면 여러 타입에서 해당 기능을 사용하고 싶을 때 프로토콜을 채택하기만 하면되는 것이다.
프로토콜 지향 언어의 장점
- SuperClass에 독립적
- 값 타입 사용 가능
- 값 타입 사용 시, 상속을 할 수 없으므로 매번 기능을 다시 구현해야 하는 한계를 극복
- 기능의 모듈화 가능
- 불필요한 API를 제외하고 정의한 API만 가져올 수 있음
ArrayList
ArrayList의 특징
리스트 자료구조는 데이터를 나란히 저장한다. 그리고 중복된 데이터의 저장을 막지 않는다.
ArrayList의 장단점
단점
- 배열의 길이가 초기에 결정되어야 한다. 변경이 불가능
- 삭제의 과정에서 데이터의 이동(복사)이 매우 빈번히 일어난다
장점
- 데이터의 참조가 쉽다. 인덱스 값을 기준으로 어디든 한번에 참조가 가능하다.
Swift로 구현된 ArrayList
import UIKit
struct arrayList {
var array: [Int?]
var numberOfData : Int
var currentPosition : Int
}
class ArrayList {
let listLength = 100
func listInit(inout list: arrayList) {
list.array = [Int?](count: listLength, repeatedValue: nil)
list.numberOfData = 0 list.currentPosition = -1
}
func listInsert(inout list: arrayList, data: Int) {
if list.numberOfData > listLength {
print("impossible Save") return
}
list.array[list.numberOfData] = data
list.numberOfData = list.numberOfData + 1
}
func listFirst(inout list: arrayList, inout data: Int) -> Bool {
if list.numberOfData == 0 { return false }
list.currentPosition = 0
data = list.array[0]!
return true
}
func listNext(inout list: arrayList, inout data: Int) -> Bool {
if list.currentPosition >= list.numberOfData - 1 { return false }
list.currentPosition = list.currentPosition + 1
data = list.array[list.currentPosition]!
return true
}
func listRemove(inout list: arrayList) -> Int? {
let removePosition = list.currentPosition
let removeData = list.array[removePosition]
for i in 0..<list.array.count-1 {
list.array[i] = list.array[i+1]
}
list.numberOfData = list.numberOfData - 1
list.currentPosition = list.currentPosition - 1
return removeData
}
func listCount(list: arrayList) -> Int { return list.numberOfData }
}
Data Source
데이터를 받아 뷰를 그려주는 역할
TableView에서 DataSource 메소드
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell // return a cell ie UITableViewCell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int // return a number ie an Int
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // return the title ie a String
Delegate
어떤 행동에 대한 동작을 제시
TabelView에서 Delegate 메소드
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: IndexPath)
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: IndexPath)
'UMC > iOS 교육' 카테고리의 다른 글
[iOS 오류] unrecognized selector sent to instance... 오류 (0) | 2022.05.26 |
---|---|
[iOS] [M1] Alamofire 설치 오류 (0) | 2022.05.12 |
[iOS] 3주차 (0) | 2022.04.04 |
[iOS] 2주차 - AutoLayout (0) | 2022.03.22 |
[iOS] 1주차 (0) | 2022.03.14 |