[Swift🦩] #11 λ©”μ„œλ“œ

λ˜μƒΒ·2022λ…„ 3μ›” 25일
0

iOS

λͺ©λ‘ 보기
23/42
post-thumbnail

클래슀, ꡬ쑰체, μ—΄κ±°ν˜• μ—μ„œ νŠΉμ • κΈ°λŠ₯을 μΊ‘μŠν™”ν•˜λŠ” μΈμŠ€ν„΄νŠΈ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  수 μžˆλ‹€.
ν”„λ‘œνΌν‹°μ²˜λŸΌ νƒ€μž…μ— κ΄€λ ¨λœ λ©”μ„œλ“œλ„ μ •μ˜ κ°€λŠ₯.
ꡬ쑰체와 μ—΄κ±°ν˜•μ—μ„œλ„ κ°€λŠ₯ν•˜λ‹€λŠ”κ²Œ C/obj-C 와 Swift 의 큰 차이점.

1. Instance Method

ν•¨μˆ˜μ™€ 문법은 λ™μΌν•˜λ‹€! κ·Έμ € 클래슀, ꡬ쑰체, μ—΄κ±°ν˜• λ‚΄μ—μ„œ μ„ μ–Έν•  뿐!

  • λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œμ— μ ‘κ·Ό κ°€λŠ₯
  • νƒ€μž… ν”„λ‘œνΌν‹°μ— μ ‘κ·Ό κ°€λŠ₯
  • μΈμŠ€ν„΄μŠ€μ—μ„œλ§Œ 호좜 κ°€λŠ₯

self property ⭐️

  • μΈμŠ€ν„΄μŠ€λŠ” μΈμŠ€ν„΄μŠ€μ™€ λ™μΌν•œ self λΌλŠ” μ•”μ‹œμ μΈ ν”„λ‘œνΌν‹° 가짐.
  • ν˜„μž¬ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘° ν•˜κΈ° μœ„ν•΄ μ‚¬μš©
  • 쓰지 μ•Šμ•„λ„ 큰 λ¬Έμ œλŠ” μ—†μ§€λ§Œ, ν•¨μˆ˜ νŒŒλΌλ―Έν„°μ™€ μΈμŠ€ν„΄μŠ€ ν”„λ‘œνΌν‹° 이름이 동일할 λ•ŒλŠ” λͺ…μ‹œν•΄μ€˜μ•Όν•œλ‹€.
var x = 10
func changeX(x: Int) {
    self.x = x
}

μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œμ—μ„œ value type μˆ˜μ • - mutating ⭐️

기본적으둜 ꡬ쑰체, μ—΄κ±°ν˜•μ˜ κ°’ νƒ€μž…μ—μ„œ μžμ‹ μ˜ ν”„λ‘œνΌν‹° μˆ˜μ •μ€ λΆˆκ°€λŠ₯ν•˜λ‹€.

  • mutating ν‚€μ›Œλ“œλ₯Ό λΆ™μ—¬μ£Όλ©΄, 변경이 κ°€λŠ₯ν•˜λ‹€.
  • μ•”μ‹œμ  self ν”„λ‘œνΌν‹°μ— μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ₯Ό ν• λ‹Ήν•œ λ’€, λ©”μ„œλ“œκ°€ μ’…λ£Œλ˜λ©΄ κΈ°μ‘΄ μΈμŠ€ν„΄μŠ€λ₯Ό λŒ€μ²΄.
  • λ‹Ήμ—°νžˆ let 이면 mutating ν‚€μ›Œλ“œλ‘œλ„ μ†λŒˆ 수 μ—†λ‹€.
struct Album {
    var price = 15000
    mutating func upPrice(to newPrice: Int) {
        price = newPrice
    }
    
    mutating func downPrice(to newPrice: Int) {
        // μ΄λ ‡κ²Œ μ•„μ˜ˆ self λ₯Ό μž¬ν• λ‹Ή 해버릴 μˆ˜λ„ μžˆλ‹€.
        self = Album(price: newPrice)
    }
}

var album = Album(price: 17000)
album.upPrice(to: 19000)

enum μ—μ„œλ„ self λ₯Ό μ΄μš©ν•œ 처리 κ°€λŠ₯!!

enum TriStateSwitch {
    case off, low, high
    mutating func next() {
        switch self {
        case .off:
            self = .low
        case .low:
            self = .high
        case .high:
            self = .off
        }
    }
}

var ovenLight = TriStateSwitch.low

ovenLight.next()
// ovenLight is now equal to .high
ovenLight.next()
// ovenLight is now equal to .off

πŸ“• self ν‚€μ›Œλ“œλ₯Ό ν†΅ν•΄μ„œ ν•΄λ‹Ή κ°’νƒ€μž… μΈμŠ€ν„΄μŠ€ 자체λ₯Ό 바꿔버릴 μˆ˜λ„ μžˆλ‹€.

mutating func reset() {
    self = LevelStruct()
}

2. Type Method ⭐️

νƒ€μž… ν”„λ‘œνΌν‹°μ²˜λŸΌ, νŠΉμ • νƒ€μž… μžμ²΄μ— ν˜ΈμΆœν•˜λŠ” λ©”μ„œλ“œ.

static

ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ μ •μ˜ν•˜λ©΄ λœλ‹€.

  • νƒ€μž… λ©”μ„œλ“œ λ‚΄μ˜ self λŠ” νƒ€μž… κ·Έ 자체λ₯Ό μ˜λ―Έν•œλ‹€.
  • λ‹€λ₯Έ νƒ€μž… λ©”μ„œλ“œμ™€ ν”„λ‘œνΌν‹°λ₯Ό μ°Έμ‘°ν•  수 μžˆλ‹€.
struct Point {
    var x: Int
    var y: Int
}

extension Point: Equatable {
    static func == (lhs: Point, rhs: Point) {
        return (lhs.x == rhs.x) && (lhs.y == rhs.y)
    }
}

class

클래슀 λ©”μ„œλ“œλŠ” class ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜ν•œλ‹€.
클래슀 μ•ˆμ—μ„œ μ •μ˜λ˜λŠ” λ©”μ„œλ“œμ΄κΈ° λ•Œλ¬Έμ— λΆ€λͺ¨μ˜ class method λ₯Ό μƒμ†ν•˜μ—¬ μ˜€λ²„λΌμ΄λ”© ν•΄μ„œ μž¬μ •μ˜ ν•  수 μžˆλ‹€.



질문

instance λ©”μ„œλ“œμ™€ class λ©”μ„œλ“œμ˜ 차이점을 μ„€λͺ…ν•˜μ‹œμ˜€.

  1. instance method : struct, class, enum 의 각 μΈμŠ€ν„΄μŠ€κ°€ κ°€μ§€λŠ” λ©”μ„œλ“œ. μ—¬κΈ°μ—μ„œ self λŠ” ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€λ₯Ό μ˜λ―Έν•˜κ²Œ λœλ‹€.
  2. class method (obj-c 의 type method) : type μžμ²΄κ°€ κ°€μ§€λŠ” λ©”μ„œλ“œλ‘œ, μ—¬κΈ°μ—μ„œ self λŠ” νƒ€μž… κ·Έ 자체λ₯Ό λ§ν•˜κ²Œ λœλ‹€.

+) μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œλŠ” μΈμŠ€ν„΄μŠ€ 생성 ν›„μ—λ§Œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€. κ·ΈλŸ¬λ‚˜ 클래슀 λ©”μ„œλ“œλŠ” ν΄λž˜μŠ€μ— μ„ μ–Έλœ λ©”μ„œλ“œμ΄λ―€λ‘œ, μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šμ•„λ„ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.


static λ©”μ„œλ“œμ™€ class λ©”μ„œλ“œμ˜ 차이점을 μ„€λͺ…ν•˜μ‹œμ˜€.

주된 차이점은 μ—­μ‹œ, class λ©”μ„œλ“œλŠ” μƒμ†μœΌλ‘œ μž¬μ •μ˜κ°€ κ°€λŠ₯ν•˜λ‹€λŠ” 점이닀.


mutating ν‚€μ›Œλ“œμ— λŒ€ν•΄ μ„€λͺ…ν•˜μ‹œμ˜€.

mutating 은 κ°’ νƒ€μž…μ΄λΌ μΈμŠ€ν„΄μŠ€ λ‚΄μ˜ 값을

point.x = 10

struct Point {
    var x: Int = 10
    var y: Int = 20
    
    func changeX(newX: Int) {
        x = newX
    }
}

이런 ν˜•νƒœλ‘œ λ°”κΏ€ 수 μ—†λŠ” ꡬ쑰체, μ—΄κ±°ν˜•μ—μ„œ μΈμŠ€ν„΄μŠ€ λ‚΄μ˜ 값을 λ°”κΎΈλŠ” λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ, ν•¨μˆ˜ μ•žμ— λΆ™μ—¬μ£ΌλŠ” ν‚€μ›Œλ“œμ΄λ‹€.

mutating func changeX(newX: Int) {
    x = newX
}

μ΄λ ‡κ²Œ ν•˜λ©΄ self에 μƒˆλ‘œμš΄ struct/enum μΈμŠ€ν„΄μŠ€λ₯Ό ν• λ‹Ήν•˜κ³ , ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€μ˜ ν”„λ‘œνΌν‹° 값을 λ°”κΎΈμ–΄μ„œ λ©”μ„œλ“œκ°€ μ’…λ£Œλ˜λ©΄ κΈ°μ‘΄ μΈμŠ€ν„΄μŠ€ λŒ€μ‹  μƒˆλ‘œμš΄(μ›ν•˜λŠ” λŒ€λ‘œ 값이 바뀐) μΈμŠ€ν„΄μŠ€λ₯Ό μ‚¬μš©ν•œλ‹€.



μ°Έκ³ 

https://bbiguduk.gitbook.io/swift/language-guide-1/methods
https://sujinnaljin.medium.com/swift-staticκ³Ό-class-method-property-효과적으둜-μ‚¬μš©ν•˜κΈ°-b336311a923c

profile
0λ…„μ°¨ iOS κ°œλ°œμžμž…λ‹ˆλ‹€.

0개의 λŒ“κΈ€