[Swift๐Ÿฆฉ] #21 ํ™•์žฅ Extension โญ๏ธโญ๏ธ

๋˜์ƒยท2022๋…„ 4์›” 7์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
33/42
post-thumbnail
  • ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•, ํ”„๋กœํ† ์ฝœ ํƒ€์ž…์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ๊ธฐ์กด ์†Œ์Šค ์ฝ”๋“œ์— ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์–ด๋„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

extension ์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ

  • computed instance property, computed type property ์ถ”๊ฐ€
  • instance method, type method ์ •์˜
  • ์ƒˆ๋กœ์šด ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ ์ œ๊ณต
  • ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ์ €์œผ์ด
  • ์ƒˆ๋กœ์šด ์ค‘์ฒฉ๋œ ํƒ€์ž… ์ •์˜์™€ ์‚ฌ์šฉ
  • ๊ธฐ์กด ํƒ€์ž…์ด ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๋„๋ก



1. Extension Syntax

extension SomeType {
    // ์ถ”๊ฐ€ํ•  ๊ธฐ๋Šฅ ์ •์˜
}

extension SomeType: SomeProtocol, AnotherProtocol {
    // ํ”„๋กœํ† ์ฝœ ํ•„์ˆ˜ ํ”„๋กœํผํ‹ฐ, ํ•จ์ˆ˜ ์ •์˜
}
  • Generic ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด, ๊ธฐ์กด ์ผ๋ฐ˜ ํƒ€์ž…์„ ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Generic + where ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด, ์ผ๋ฐ˜ ํƒ€์ž…์„ ํ™•์žฅํ•ด์„œ ์กฐ๊ฑด๋ถ€๋กœ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ.



2. Computed Properties

  • ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ๋‚˜ ๊ธฐ์กด ํ”„๋กœํผํ‹ฐ์— ๊ด€์ฐฐ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š”๊ฑด ๋ถˆ๊ฐ€๋Šฅ.
extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// Prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// Prints "Three feet is 0.914399970739201 meters"



3. Initializers

  • ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค๋ฅธ ํƒ€์ž…์„ ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉ์ž ์ •์˜ ํƒ€์ž…์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ๊ธฐ์กด ํƒ€์ž…์˜ init ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ ์ดˆ๊ธฐํ™”
  • ํด๋ž˜์Šค
    • convenience init ๋งŒ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ.
    • designated init ์ด๋‚˜ deinit ์€ ๋ถˆ๊ฐ€๋Šฅ.
  • ๊ฐ’ํƒ€์ž…
    • extension ์„ ๊ฐ’ํƒ€์ž…์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ• ๊ฑฐ๋ผ๋ฉด,
    • ๊ฐ’ํƒ€์ž…์ด ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด ๊ธฐ๋ณธ๊ฐ’ ์ œ๊ณต & ์ปค์Šคํ…€ ์ด๋‹ˆ์…œ๋ผ์ด์ € ์ •์˜ X
      -> ๊ธฐ๋ณธ init, memberwise init ์ œ๊ณต
    • ๊ทธ ๊ธฐ๋ณธ init, memberwise init ์„ ๋ชจ๋‘ extension ์—์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ธฐ์กด init ํ˜ธ์ถœ ํ›„์— self ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ.
// ์ดˆ๊ธฐ๊ฐ’์ด ์žˆ๊ณ  custom init ์—†์Œ
struct Size {
    var width = 0.0, height = 0.0
}
struct Point {
    var x = 0.0, y = 0.0
}
struct Rect {
    var origin = Point()
    var size = Size()
}

let defaultRect = Rect()
let memberwiseRect = Rect(origin: Point(x: 2.0, y: 2.0),
   size: Size(width: 5.0, height: 5.0))


extension Rect {
    // ๊ฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ด๋‹ˆ์…œ๋ผ์ด์ €๊ฐ€ ํ•œ๋ฒˆ ์™„๋ฃŒ๋˜๋ฉด ์ดˆ๊ธฐํ™”๋˜๋„๋ก ํ™•์‹คํ•˜๊ฒŒ ์งœ์•ผํ•จ.
    init(center: Point, size: Size) {
        let originX = center.x - (size.width / 2)
        let originY = center.y - (size.height / 2)
        self.init(origin: Point(x: originX, y: originY), size: size)
    }
}



4. Methods

  • ๋ฉ”์„œ๋“œ, ํƒ€์ž… ๋ฉ”์„œ๋“œ ์ •์˜ ๊ฐ€๋Šฅ.
extension Int {
    func repetitions(task: () -> Void) {
        for _ in 0..<self {
            task()
        }
    }
}

3.repetitions { print("wink") }

mutating method

extension Int {
    mutating func square() {
        self = self * self
    }
}
var someInt = 3
someInt.square()
// someInt is now 9



5. Subscripts

  • ์กด์žฌํ•˜๋Š” ํƒ€์ž…์— ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ.
extension Int {
    subscript(digitIndex: Int) -> Int {
        var decimalBase = 1
        for _ in 0..<digitIndex {
            decimalBase *= 10
        }
        return (self / decimalBase) % 10
        // 10 * n๋ฒˆ์งธ ์ˆ˜๋กœ ํ˜„์žฌ ์ˆ˜๋ฅผ ๋‚˜๋ˆˆ ๊ฒƒ์˜ ๋‚˜๋จธ์ง€
      // 1์ธ ๊ฒฝ์šฐ 746381295 % 10 -> 5๊ฐ€ ๋‚˜๋จธ์ง€
      // 2์ธ ๊ฒฝ์šฐ 746381295 % 10 -> 9๊ฐ€ ๋‚˜๋จธ์ง€
    }
}
746381295[0] // 5
746381295[1] // 9
746381295[2] // 2
746381295[8] // 7
746381295[9] // 0



6. Nested Types

  • extension ์„ ์ด์šฉํ•ด์„œ ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์— ์ค‘์ฒฉ ํƒ€์ž… ์ถ”๊ฐ€ ๊ฐ€๋Šฅ.
extension Int {
    enum Kind {
        case negative, zero, positive
    }
    var kind: Kind {
        switch self {
        case 0:
            return .zero
        case let x where x > 0:
            return .positive
        default:
            return .negative
        }
    }
}


func printIntegerKinds(_ numbers: [Int]) {
    for number in numbers {
        switch number.kind {
        case .negative:
            print("- ", terminator: "")
        case .zero:
            print("0 ", terminator: "")
        case .positive:
            print("+ ", terminator: "")
        }
    }
    print("")
}
printIntegerKinds([3, 19, -27, 0, -6, 0, 7])
// Prints "+ + - 0 - 0 + "



์งˆ๋ฌธ

Extension์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค.

  • Extension ์€ ๊ธฐ์กด ํƒ€์ž…์„ ํ™•์žฅํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
  • ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ, ํƒ€์ž… ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ, ํƒ€์ž… ๋ฉ”์„œ๋“œ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ, ์ค‘์ฒฉํƒ€์ž…, ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,
    • ์ €์žฅ ํ”„๋กœํผํ‹ฐ์™€ ํ”„๋กœํผํ‹ฐ์— ์˜ต์ €๋ฒ„ ์ถ”๊ฐ€๋Š” ๋ถˆ๊ฐ€๋Šฅ.
    • ์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ํด๋ž˜์Šค์—์„  convenience ๋งŒ ์ •์˜ ๊ฐ€๋Šฅ ํ•˜๊ณ ,
    • ๊ฐ’ ํƒ€์ž…์—์„  ๋ชจ๋“  ๋ฉค๋ฒ„์— ๊ธฐ๋ณธ๊ฐ’ ์žˆ๊ณ  ์ดˆ๊ธฐ์ž ์—†์„ ๋•Œ, ๊ธฐ๋ณธ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” init / memberwise init ์„ extension ์—์„œ๋„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ํ˜ธ์ถœํ•˜๊ธฐ ์ „์—” self ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ.
  • ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋Š” ํƒ€์ž…์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ์šฉ๋„๋กœ๋„ ์‚ฌ์šฉํ•œ๋‹ค.

Extension ๋‚ด๋ถ€์—์„œ ํ•จ์ˆ˜๋ฅผ overrideํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์„ค๋ช…ํ•˜์‹œ์˜ค.

  • Swift ์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋˜์–ด์žˆ์œผ๋‚˜,
  • Obj-C ์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๊ทธ๋ž˜์„œ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ Obj-C ์™€์˜ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด override ๋ฅผ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋‹จ ๋œ๋‹ค.
  • ๊ทธ์น˜๋งŒ ์ง€์–‘ํ•˜์ž.

๋ฉด์ ‘ ์Šคํ„ฐ๋”” ํ›„ ์ถ”๊ฐ€.

  • override ์˜ ๊ฒฝ์šฐ๋Š” ์ˆ˜์ง์ ์ธ ํ™•์žฅ์ด๊ณ ,
  • extension ์€ ์–ด๋–ค ํƒ€์ž…์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜ํ‰์ ์ธ ํ™•์žฅ์ด๋‹ค.
  • ๋‘˜ ๋‹ค ํ™•์žฅ์ธ ๊ฒƒ์€ ๋งž์ง€๋งŒ. ๊ฐ™์€ ํƒ€์ž…์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ‰์ ์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ณณ์—์„œ ์ˆ˜์ง์  ํ™•์žฅ์ธ override ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋งž์ง€ ์•Š๋‹ค.



์ถœ์ฒ˜
https://bbiguduk.gitbook.io/swift/language-guide-1/extensions
https://feather.tistory.com/2

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€