λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
⌨️ Language/swift

[Swift] ν”„λ‘œν† μ½œ

by hyebin (Helia) 2024. 3. 29.
λ°˜μ‘ν˜•

ν”„λ‘œν† μ½œ(Protocols)

  • μ–΄λ–€ κΈ°λŠ₯에 ν•„μš”ν•œ μš”κ΅¬μ‚¬ν•­μ„ μ„ μ–Έν•΄λ‘λŠ” 것
  • 클래슀, ꡬ쑰체, μ—΄κ±°ν˜•μ— μ˜ν•΄ 채택

ν”„λ‘œν† μ½œ 문법

protocol SomeProtocol {
// protocol definition
}

ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ” νƒ€μž…μ„ μ •μ˜ν•˜κΈ° μœ„ν•΄μ„œλŠ” νƒ€μž… 이름 뒀에 콜둠(:)을 뢙이고 ν”„λ‘œν† μ½œ 이름을 적음

ν”„λ‘œν† μ½œμ„ 채택할 λ•ŒλŠ” ν”„λ‘œν† μ½œμ— μ •μ˜λœ μš”κ΅¬μ‚¬ν•­μ„ λ°˜λ“œμ‹œ κ΅¬ν˜„ ν•΄μ•Όλ§Œ 함

 

struct SomeStructure: FirstProtocol, AnotherProtocol {
    // structure definition goes here
}

class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {
    // class definition goes here
}

ν”„λ‘œν† μ½œ μš”κ΅¬μ‚¬ν•­ μ •μ˜

ν”„λ‘œνΌν‹° μš”κ΅¬μ‚¬ν•­

  • ν”„λ‘œνΌν‹°μ˜ 이름과 νƒ€μž… 그리고 gettable, settable ν•œμ§€ λͺ…μ‹œν•΄μ•Ό 함
  • μ—°μ‚° ν”„λ‘œνΌν‹°λŠ” 항상 var둜 선언해야함(μ €μž₯ ν”„λ‘œνΌν‹°λŠ” gettable 만 κ°€λŠ₯ν•œ κ²½μš°μ—λŠ” let으둜 μ„ μ–Έ κ°€λŠ₯), νƒ€μž… ν”„λ‘œνΌν‹°λŠ” static ν‚€μ›Œλ“œ μ‚¬μš©
protocol SomeProtocol {
    var mustBeSettable: Int { get set }
    var doesNotNeedToBeSettable: Int { get }
		static var someTypeProperty: Int { get set }
}

 

λ©”μ†Œλ“œ μš”κ΅¬μ‚¬ν•­

  • ν•„μˆ˜ μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œμ™€ νƒ€μž… λ©”μ†Œλ“œ λͺ…μ‹œ κ°€λŠ₯
  • λ©”μ†Œλ“œ νŒŒλΌλ―Έν„°μ˜ 기본값은 μ‚¬μš© λΆˆκ°€
protocol SomeProtocol {
    static func someTypeMethod()
    func random() -> Double
}

 

λ³€κ²½ κ°€λŠ₯ν•œ λ©”μ†Œλ“œ μš”κ΅¬μ‚¬ν•­

  • mutating ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ μΈμŠ€ν„΄μŠ€μ—μ„œ λ³€κ²½ κ°€λŠ₯ν•˜λ‹€λŠ” 것을 ν‘œμ‹œ
  • mutating ν‚€μ›Œλ“œλŠ” κ°’ νƒ€μž…μ—μ„œλ§Œ μ‚¬μš©
protocol Togglable {
    mutating func toggle()
}

 

초기자 μš”κ΅¬μ‚¬ν•­

  • ν”„λ‘œν† μ½œμ—μ„œ ν•„μˆ˜λ‘œ κ΅¬ν˜„ν•΄μ•Όν•˜λŠ” μ΄λ‹ˆμ…œλΌμ΄μ € μ§€μ • κ°€λŠ₯
  • κ΅¬ν˜„ν•  λ•Œ μ΄λ‹ˆμ…œλΌμ΄μ € μ•žμ— required ν‚€μ›Œλ“œλ₯Ό λΆ™μ—¬μ€˜μ•Όν•¨
protocol SomeProtocol {
    init(someParameter: Int)
}

class SomeClass: SomeProtocol {
    required init(someParameter: Int) {
        // initializer implementation goes here
    }
}
  • ν”„λ‘œν† μ½œμ„ μ±„νƒν•˜κ³  클래슀λ₯Ό μƒμ†λ°›λŠ” 경우 μ΄λ‹ˆμ…œλΌμ΄μ € μ•žμ— required ν‚€μ›Œλ“œμ™€ override ν‚€μ›Œλ“œλ₯Ό 적어야함
protocol SomeProtocol {
    init()
}

class SomeSuperClass {
    init() {
        // initializer implementation goes here
    }
}

class SomeSubClass: SomeSuperClass, SomeProtocol {
    // "required" from SomeProtocol conformance; "override" from SomeSuperClass
    required override init() {
        // initializer implementation goes here
    }
}

 

νƒ€μž…μœΌλ‘œμ¨μ˜ ν”„λ‘œν† μ½œ

ν”„λ‘œν† μ½œλ„ νƒ€μž…μœΌλ‘œ μ‚¬μš©κ°€λŠ₯

  • ν•¨μˆ˜, λ©”μ†Œλ“œ, μ΄λ‹ˆμ…œλΌμ΄μ €μ˜ νŒŒλΌλ―Έν„° νƒ€μž… ν˜Ήμ€ 리턴 νƒ€μž…
  • μƒμˆ˜, λ³€μˆ˜, ν”„λ‘œνΌν‹°μ˜ νƒ€μž…
  • μ»¨ν…Œμ΄λ„ˆμΈ λ°°μ—΄, 사전 λ“±μ˜ μ•„μ΄ν…œ νƒ€μž…

ν”„λ‘œν† μ½œ μœ„μž„

클래슀 ν˜Ήμ€ ꡬ쑰체 μΈμŠ€ν„΄μŠ€μ— νŠΉμ • ν–‰μœ„μ— λŒ€ν•œ μ±…μž„μ„ λ„˜κΈΈ 수 있게 ν•΄μ£ΌλŠ” λ””μžμΈ νŒ¨ν„΄

 

μ΅μŠ€ν…μ…˜μ˜ μ‚¬μš©

  • 이미 μ‘΄μž¬ν•˜λŠ” νƒ€μž…μ— μƒˆ ν”„λ‘œν† μ½œμ„ λ”°λ₯΄κ²Œ ν•˜κΈ°μœ„ν•΄ μ΅μŠ€ν…μ…˜μ„ μ‚¬μš©
  • μ›λž˜ 값에 μ ‘κ·Ό κΆŒν•œμ΄ 없어도 μ΅μŠ€ν…μ…˜μ„ μ‚¬μš©ν•΄ κΈ°λŠ₯ ν™•μž₯ κ°€λŠ₯
protocol TextRepresentable {
    var textualDescription: String { get }
}

 

쑰건적으둜 ν”„λ‘œν† μ½œ λ”°λ₯΄κΈ°

where ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ νŠΉμ • 쑰건을 λ§Œμ‘±μ‹œν‚¬ λ•Œλ§Œ ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λ„둝 μ œν•œ

 

ν”„λ‘œν† μ½œ νƒ€μž… μ»¬λ ‰μ…˜

ν”„λ‘œν† μ½œμ„ collection νƒ€μž…μ— λ„£κΈ°μœ„ν•œ νƒ€μž…μœΌλ‘œ μ‚¬μš©κ°€λŠ₯

let things: [TextRepresentable] = [game, d12, simonTheHamster]

for thing in things {
    print(thing.textualDescription)
}
// A game of Snakes and Ladders with 25 squares
// A 12-sided dice
// A hamster named Simon

 

ν”„λ‘œν† μ½œ 상속

클래슀 상속같이 ν”„λ‘œν† μ½œλ„ 상속 κ°€λŠ₯, 콀마(,)둜 ꡬ뢄

protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
    // protocol definition goes here
}

 

클래슀 μ „μš© ν”„λ‘œν† μ½œ

ν”„λ‘œν† μ½œμ— AnyObjectλ₯Ό μΆ”κ°€

protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
    // class-only protocol definition goes here
}

 

ν”„λ‘œν† μ½œ ν•©μ„±

λ™μ‹œμ— μ—¬λŸ¬ ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ” νƒ€μž…μ„ μ„ μ–Έ κ°€λŠ₯

protocol Named {
    var name: String { get }
}
protocol Aged {
    var age: Int { get }
}
struct Person: Named, Aged {
    var name: String
    var age: Int
}
func wishHappyBirthday(to celebrator: Named & Aged) {
    print("Happy birthday, \\(celebrator.name), you're \\(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(to: birthdayPerson)
// Prints "Happy birthday, Malcolm, you're 21!"

 

ν”„λ‘œν† μ½œ μˆœμ‘ 확인

μ–΄λ–€ νƒ€μž…μ΄ νŠΉμ • ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ”μ§€ λ‹€μŒκ³Ό 같은 λ°©λ²•μœΌλ‘œ 확인 κ°€λŠ₯

  • isμ—°μ‚°μžλ₯Ό μ΄μš©ν•˜λ©΄ μ–΄λ–€ νƒ€μž…μ΄ νŠΉμ • ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ”μ§€ 확인가λŠ₯
    • νŠΉμ • ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λ©΄ trueλ₯Ό μ•„λ‹ˆλ©΄ falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • as?λŠ” νŠΉμ • ν”„λ‘œν† μ½œ νƒ€μž…μ„ λ”°λ₯΄λŠ” 경우 κ·Έ μ˜΅μ…”λ„ νƒ€μž…μ˜ ν”„λ‘œν† μ½œ νƒ€μž…μœΌλ‘œ λ‹€μš΄μΊμŠ€νŠΈλ₯Ό ν•˜κ²Œ 되고 λ”°λ₯΄μ§€ μ•ŠλŠ” κ²½μš°λŠ” nil을 λ°˜ν™˜
  • as!λŠ” κ°•μ œλ‘œ νŠΉμ • ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λ„둝 μ •μ˜
    • λ§Œμ•½ λ‹€μš΄μΊμŠ€νŠΈμ— μ‹€νŒ¨ν•˜λ©΄ λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒ

선택적 ν”„λ‘œν† μ½œ μš”κ΅¬μ‘°κ±΄

  • 선택적 κ΅¬ν˜„ 쑰건을 μ •μ˜ κ°€λŠ₯
  • @objc ν‚€μ›Œλ“œλ₯Ό ν”„λ‘œν† μ½œ μ•žμ— 뢙이고 κ°œλ³„ ν•¨μˆ˜ ν˜Ήμ€ ν”„λ‘œνΌν‹°μ—λŠ” @objc 와 optional ν‚€μ›Œλ“œ μ‚¬μš©
    • @objc ν”„λ‘œν† μ½œμ€ 클래슀 νƒ€μž…μ—μ„œλ§Œ μ‚¬μš©λ  수 있고 κ΅¬μ‘°μ²΄λ‚˜ μ—΄κ±°ν˜•μ—μ„œλŠ” μ‚¬μš©ν•  수 μ—†μŒ
@objc protocol CounterDataSource {
    @objc optional func increment(forCount count: Int) -> Int
    @objc optional var fixedIncrement: Int { get }
}

 

ν”„λ‘œν† μ½œ μ΅μŠ€ν…μ…˜

μ΅μŠ€ν…μ…˜μ„ μ‚¬μš©ν•΄ ν”„λ‘œν† μ½œ ν™•μž₯κ°€λŠ₯

κΈ°λ³Έ κ΅¬ν˜„ 제곡

  • μ΅μŠ€ν…μ…˜μ„ κΈ°λ³Έ κ΅¬ν˜„μ„ μ œκ³΅ν•˜λŠ”λ° μ‚¬μš© κ°€λŠ₯
  • ν”„λ‘œν† μ½œμ—μ„œλŠ” μ„ μ–Έλ§Œ ν•  수 μžˆλŠ”λ° μ΅μŠ€ν…μ…˜μ„ μ΄μš©ν•΄ κΈ°λ³Έ κ΅¬ν˜„μ„ 제곡
extension PrettyTextRepresentable  {
    var prettyTextualDescription: String {
        return textualDescription
    }
}

ν”„λ‘œν† μ½œ μ΅μŠ€ν…μ…˜μ— μ œμ•½ μΆ”κ°€

  • μ΅μŠ€ν…μ…˜μ΄ νŠΉμ • μ‘°κ±΄μ—μ„œλ§Œ μ μš©λ˜λ„λ‘ where μ ˆμ„ μ‚¬μš©ν•΄ μ„ μ–Έ κ°€λŠ₯
extension Collection where Element: Equatable {
    func allEqual() -> Bool {
        for element in self {
            if element != self.first {
                return false
            }
        }
        return true
    }
}
λ°˜μ‘ν˜•

'⌨️ Language > swift' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Swift] μžλ™ μ°Έμ‘° 카운트  (0) 2024.04.02
[Swift] μ œλ„€λ¦­  (0) 2024.04.02
[Swift] μ—λŸ¬ 처리  (0) 2024.03.28
[Swift] μ˜΅μ…”λ„ 체이닝  (0) 2024.03.26
[Swift] μ΄ˆκΈ°ν™”  (1) 2024.03.25