property 학습하기 (3) [ Type Property ]

2024. 8. 25. 18:49·Programing Langauge/swift
반응형

 

이번에는 타입 프로퍼티의 역할을 학습해보자.

 

Type Property (타입 프로퍼티)

모든 타입이 공통적인 값을 정의하는 역할을 한다.

 

특징

1) 클래스, 구조체, 열거형에서 사용된다

 

2) 저장 타입 프로퍼티와 연산 타입 프로퍼티가 존재하며 저장 타입 프로퍼티의 경우 항상 초기화가 되어 있어야 한다

 

3) static을 이용해 선언하며, 자동으로 lazy로 작동한다

 

종류

  static class
저장 프로퍼티 사용 가능 O X
연산 프로퍼티로 사용 가능 O X
오버라이딩 가능  X O

예시 코드

class Human {
  
  
    static var typeStoredProperty1: String = "HELLO"
    
    // ❌ 에러발생
    class var typeStoredProperty2: String = "HELLO2" // class는  저장으로 사용불가
    
    static var typeComputedProperty1: String {
        return typeStoredProperty1
    }
    
    class var typeComputedProperty2: String {
        return typeStoredProperty1
    }

}


class SmallHuman : Human {
    
    
    static var typeStoredProperty3 = "HELLO3"
    
    override class var typeComputedProperty2: String {
        return typeStoredProperty3
    }
    
    // ❌ 에러발생 , typeStoredProperty1는 static이라 override 불가
    override static var typeStoredProperty1: String {
        return typeStoredProperty3
    }
}

 

반응형

'Programing Langauge > swift' 카테고리의 다른 글

접근 제한자  (2) 2024.08.25
property 학습하기 (4) [ Property Observer ]  (0) 2024.08.25
property 학습하기 (2) [ computed Property ]  (0) 2024.08.25
property 학습하기 (1) [ Stored Property ]  (0) 2024.08.25
protocol 학습하기 (4) [ any, some ]  (1) 2024.08.25
'Programing Langauge/swift' 카테고리의 다른 글
  • 접근 제한자
  • property 학습하기 (4) [ Property Observer ]
  • property 학습하기 (2) [ computed Property ]
  • property 학습하기 (1) [ Stored Property ]
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (297)
      • CS (29)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (5)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (36)
        • swift (31)
        • python (4)
        • Kotlin (1)
      • iOS (131)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (10)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (9)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    protocol
    Swift
    백준
    dispatch
    SwiftUI
    Network
    dfs
    UIKit
    lifecycle
    property
    AVFoundation
    IOS
    boostcamp
    CS
    Tuist
    dp
    GIT
    프로그래머스
    투포인터
    concurrency
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
property 학습하기 (3) [ Type Property ]
상단으로

티스토리툴바