Protocol์˜ ๊ฒฝ์šฐ POP๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ Apple์—์„œ ๋ฐ€์–ด์ฃผ๋Š”? ๊ฐœ๋…์ด๋‹ค.

Protocol

  • ์–ด๋–ค ์ž‘์—…/๊ธฐ๋Šฅ์„ ์œ„ํ•œ method, property์˜ interface ์ •์˜์‹œ ์‚ฌ์šฉ
  • class, struct, enum์— ์ฑ„ํƒ๋˜๋ฉฐ ๊ทธ๋•Œ ํ•ด๋‹น type์€ ์š”๊ตฌ ์‚ฌํ•ญ์„ ๊ตฌํ˜„ ํ•ด์•ผ ํ•จ
  • ์ด๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ, type์ด protocol์„ ์ค€์ˆ˜ํ•œ๋‹ค๊ณ  ํ•จ

Objective-C์— ๋น„ํ•ด ๋‚˜์•„์ง„ ์ 

  • type์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • protocol extension์„ ์ด์šฉํ•˜์—ฌ default implementation ๊ฐ€๋Šฅ
    • ์ค€์ˆ˜ํ•˜๋Š” ๋ชจ๋“  type์ด ์ถ”๊ฐ€์ž‘์—… ์—†์ด ํ™•์žฅ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง
  • protocol ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ POP๋ผ ๋ถ€๋ฆ„
protocol SomeProtocol {
var mustBeSettable: Int { get, set }
var doesNotNeedToBeSettable: Int { get }
}

Protocol Type

  • ๋ชจ๋“  type๊ณผ ๋™์ผํ•œ ๋™์ž‘ ๊ฐ€๋Šฅ
    • return type ๊ฐ€๋Šฅ
    • property๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • container(array, dictionary etc)์˜ element๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

Inheritance

  • ๊ธฐ์กด ์กด์žฌํ•˜๋Š” ๋‹ค๋ฅธ protocol์„ ์ƒ์†๋ฐ›์•„ ํ™•์žฅ ๊ฐ€๋Šฅ
  • class์™€ ๋‹ฌ๋ฆฌ multiple inheritance ๊ฐ€๋Šฅ

Class-only Protocol

  • class์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋„๋ก ์ œ์•ฝ ๊ฐ€๋Šฅ
    protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
        // statement
    }

Protocol Composition

  • ๋‘๊ฐœ ์ด์ƒ protocol์„ ๋งŒ์กฑํ•˜๋Š” type์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ
    protocol Named {
        var name: String { get }
    }
     
    protocol Aged {
        var age: Int { get }
    }
     
    struct Person: Named, Aged {
        var name: String
        var age: Int
    }
     
    func withHappyBirthday(to celebrator: Named & Aged) {
        // statement
    }

is, as ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ

  • is
    • protocol conform์‹œ True
  • as
    • as?
    • protocol conformํ•˜์ง€ ์•Š์œผ๋ฉด nil
  • as!
    • protocol conformํ•˜์ง€ ์•Š์œผ๋ฉด runtime error

Optional Protocol Requirement

  • Objective-C์—์„œ ์ฒ˜๋Ÿผ Optional ์ •์˜ ๊ฐ€๋Šฅ
  • ๋‹จ, Objective-C class ์ƒ์† ๋ฐ›์€ class๋งŒ ์ฑ„ํƒ๊ฐ€๋Šฅ
  • @objc attribute ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ
  • @objc optional var ...
  • @objc optional func ...

Protocol Constraint

  • protocol์„ conformํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์— ๋Œ€ํ•œ ์ œ์•ฝ์„ ๊ฑธ ์ˆ˜ ์ด์”…ใ…
  • where ์‚ฌ์šฉ
protocol RandomBAckgroundColorView where Self: UIView {
	// statement
}

Protocol Extension

Default function

extension RandomNumberGenerator {
	func randomBool() -> Bool {
	  return random() > 0.5
	}
}

Default Value

extension CounterDataSource {
	var fixedIncrement: Int {
	  return 0
	}
}
  • extension์œผ๋กœ default value๋ฅผ ๊ตฌํ˜„ํ•ด๋‘๋ฉด, ํ•„์š”์‹œ์—๋งŒ ์ถ”๊ฐ€ ๊ตฌํ˜„ํ•˜๋„๋ก ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ
  • ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ optional์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Œ
    • @objc attribute ์ง€์šธ ์ˆ˜ ์žˆ์Œ

Constraint ์ถ”๊ฐ€

  • ํ™•์žฅ์‹œ ์ œ์•ฝ์„ ๊ฑธ ์ˆ˜ ์žˆ์Œ

  • where ์‚ฌ์šฉ

  • protocol ์ž์ฒด ๊ธฐ๋Šฅ (๋‚ด๋ถ€์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”)๋งŒ ์ด์šฉํ•ด์„œ ๊ธฐ๋Šฅ ํ™•์žฅ ํ•ด์•ผํ•จ

  • ๊ธฐ์กด์— ๊ทธ protocol์„ ์ค€์ˆ˜ํ•˜๋Š” ๋ชจ๋“  type์ด ํ™•์žฅ๋œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ฒŒ ๋จ

    extension Collection where Element: Equatable {
        func allEqual() -> Bool {
      	  for element in self {
      		  if element != self.first {
      			  return False
      		  }
      	  }
        }
    }