ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] ν”„λ‘œνΌν‹°(property)
    iOS/Swift 2022. 8. 2. 21:53

     

    πŸ’‘ ν”„λ‘œνΌν‹°: 클래슀, ꡬ쑰체, μ—΄κ±°ν˜• 등에 κ΄€λ ¨λœ κ°’

     

    ν”„λ‘œνΌν‹°μ˜ μ’…λ₯˜

    μ €μž₯ ν”„λ‘œνΌν‹° μ—°μ‚° ν”„λ‘œνΌν‹° νƒ€μž… ν”„λ‘œνΌν‹°
    μΈμŠ€ν„΄μŠ€μ˜ λ³€μˆ˜ λ˜λŠ” μƒμˆ˜ νŠΉμ • 연산을 μ‹€ν–‰ν•œ κ²°κ³Όκ°’ νŠΉμ • νƒ€μž…μ— μ‚¬μš©λ˜λŠ”  ν”„λ‘œνΌν‹°

    + ν”„λ‘œνΌν‹° κ°μ‹œμž

     


    μ €μž₯ ν”„λ‘œνΌν‹°

    - 클래슀 λ˜λŠ” ꡬ쑰체의 μΈμŠ€ν„΄μŠ€μ™€ μ—°κ΄€λœ 값을 μ €μž₯ν•œλ‹€

    - κΈ°λ³Έκ°’κ³Ό μ΄ˆκΈ°κ°’ 지정O

     

    πŸ’‘ 클래슀의 경우 μ΄ˆκΈ°κ°’μ„ 지정해주지 μ•ŠμœΌλ©΄ λ”°λ‘œ μ •μ˜ν•΄μ€˜μ•Ό ν•œλ‹€
    πŸ’‘ μ˜΅μ…”λ„ μ €μž₯ ν”„λ‘œνΌν‹°λ₯Ό 톡해 값을 μ΄ˆκΈ°κ°’μ„ 넣어주지 μ•Šκ³  λ‚˜μ€‘μ— 값이 μžˆμ„ 경우 ν• λ‹Ήν•  μˆ˜λ„ μžˆλ‹€
    // ============ < μ €μž₯ ν”„λ‘œνΌν‹° > ============
    
    // ꡬ쑰체
    struct CoordinatePoint {
        // μ €μž₯ ν”„λ‘œνΌν‹°
        var x: Int
        var y: Int
    }
    
    // μΈμŠ€ν„΄μŠ€ 생성
    let yeonsuPoint: CoordinatePoint = CoordinatePoint(x: 10, y: 5)
    
    
    // 클래슀
    class Position {
        var point: CoordinatePoint
        let name: String
    
        // 클래슀의 경우 μ΄ˆκΈ°κ°’μ„ 지정해주지 μ•ŠμœΌλ©΄ λ”°λ‘œ μ •μ˜ν•΄μ€˜μ•Ό 함
        init(name: String, CurrentPoint: CoordinatePoint){
            self.name = name
            self.point = CurrentPoint
        }
    }
    
    print("yeonsu's position is \(yeonsuPoint.x), \(yeonsuPoint.y)")
    
    
    
    // μ˜΅μ…”λ„ μ €μž₯ ν”„λ‘œνΌν‹°
    class Yeonsu {
        var name: String
        var point: CoordinatePoint? //μ˜΅μ…”λ„λ‘œ 지정
    
    
        init(name: String) {
            self.name = name
        }
    }
    
    let introduceYs: Yeonsu = Yeonsu(name: "Bonny")
    
    introduceYs.point = CoordinatePoint(x:10, y:2)  //값을 λ‚˜μ€‘μ— ν• λ‹Ή
    
    print(introduceYs.name)

    지연 μ €μž₯ ν”„λ‘œνΌν‹°

    - 처음 μ‚¬μš©ν•  λ•ŒκΉŒμ§€ μ΄ˆκΈ°κ°’μ΄ κ³„μ‚°λ˜μ§€ μ•Šμ€ ν”„λ‘œνΌν‹°

    - 호좜이 μžˆμ–΄μ•Ό 값을 μ΄ˆκΈ°ν™”ν•œλ‹€

     

    1. var ν‚€μ›Œλ“œ μ‚¬μš©

    2. lazy ν‚€μ›Œλ“œ μ‚¬μš©

     

    struct Coordinate {
        var x: Int = 0
        var y: Int = 0
    }
    
    class Position_p {
        lazy var point: CoordinatePoint = CoordinatePoint(x: 1, y: 2)
    }
    
    let ysPosition: Position_p = Position_p()
    
    print(ysPosition.point)     //처음 CoordinatePoint 생성

    μ—°μ‚° ν”„λ‘œνΌν‹°

    - νŠΉμ • μƒνƒœμ— λ”°λ₯Έ 값을 μ—°μ‚°ν•œλ‹€

    - μ ‘κ·Όμž(getter)와 μ„€μ •μž(setter)의 μ—­ν•  μˆ˜ν–‰

     

    struct Coordinate2 {
        
        // μ €μž₯ ν”„λ‘œνΌν‹°
        var x: Int
        var y: Int
        
        // μ—°μ‚° ν”„λ‘œνΌν‹°
        var oppositePoint: Coordinate2 {
            //μ ‘κ·Όμž
            get {
                return Coordinate2(x: -x, y: -y)
            }
            
            //μ„€μ •μž
            set(opposite) {
                x = -opposite.x
                y = -opposite.y
            }
        }
    }
    
    var yeonsuPosition: Coordinate2 = Coordinate2(x: 10, y: 30)
    
    print(yeonsuPosition)
    print(yeonsuPosition.oppositePoint)

    ν”„λ‘œνΌν‹° κ°μ‹œμž

    - ν”„λ‘œνΌν‹° 값이 변경됨에 따라 ν˜ΈμΆœν•΄μ„œ ν–‰ν•˜κ³  싢은 μž‘μ—…μ΄ μžˆμ„ λ•Œ μ‚¬μš©ν•œλ‹€

    willSet λ©”μ„œλ“œ didSet λ©”μ„œλ“œ
    ν”„λ‘œνΌν‹° 값이 λ³€κ²½λ˜κΈ° 직전에 호좜 ν”„λ‘œνΌν‹° 값이 λ³€κ²½λœ 직후에 호좜
    인자: ν”„λ‘œνΌν‹°κ°€ 변경될 κ°’ 인자: ν”„λ‘œνΌν‹°κ°€ λ³€κ²½λ˜κΈ° μ „μ˜ κ°’
    λ§€κ°œλ³€μˆ˜ λ””ν΄νŠΈ 이름: newValue λ§€κ°œλ³€μˆ˜ λ””ν΄νŠΈ 이름: OldValue

     

    class Account {
        var credit: Int = 0 {
            willSet {
                print("μž”μ•‘μ΄ \(credit)μ›μ—μ„œ \(newValue)μ›μœΌλ‘œ 변경될 μ˜ˆμ •μž…λ‹ˆλ‹€")
            }
            
            didSet {
                print("μž”μ•‘μ΄ \(oldValue)μ›μ—μ„œ \(credit)μ›μœΌλ‘œ λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€")
            }
        }
    }
    
    let myAccount: Account = Account()  //μΈμŠ€ν„΄μŠ€ 생성
    //print("μž”μ•‘μ΄ \(credit)μ›μ—μ„œ \(newValue)μ›μœΌλ‘œ 변경될 μ˜ˆμ •μž…λ‹ˆλ‹€
    
    myAccount.credit = 1000     //μΈμŠ€ν„΄μŠ€ μ‚¬μš©
    //print("μž”μ•‘μ΄ \(oldValue)μ›μ—μ„œ \(credit)μ›μœΌλ‘œ λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€

     

    전체 μ½”λ“œ 좜λ ₯ κ²°κ³Ό

Designed by Tistory.