본문 바로가기

IOS 개발

[iOS] Self

 

# Instance Method #

# self( )  #

 

수신자를 반환합니다.

Returns the receiver.

 

필수입니다.

Required.

 

선언

Declaration

 

func `self`() -> Self

 

반환값

Return Value

 

수신자

The receiver.

 

 

Self Type

 

Self 유형은 특정 유형이 아니라 해당 유형의 이름을 반복하거나 알지 않고도 편리하게 현재 유형을 참조 할 수 있습니다.

The Self type isn’t a specific type, but rather lets you conveniently refer to the current type without repeating or knowing that type’s name.

 

프로토콜 선언 또는 프로토콜 멤버 선언에서 자체 유형은 프로토콜을 따르는 최종 유형을 나타냅니다.

In a protocol declaration or a protocol member declaration, the Self type refers to the eventual type that conforms to the protocol.

 

구조, 클래스 또는 열거 선언에서 Self 유형은 선언에 의해 도입 된 유형을 나타냅니다. 

형식 멤버에 대한 선언에서 자체 형식은 해당 형식을 나타냅니다. 

클래스 선언의 멤버에서 Self는 메소드의 리턴 유형으로, 메소드 본문에 표시 될 수 있지만 

다른 문맥에서는 표시되지 않습니다. 

예를 들어 아래 코드는 반환 유형이 Self 인 인스턴스 메서드 f를 보여줍니다.

In a structure, class, or enumeration declaration, the Self type refers to the type introduced by the declaration. Inside the declaration for a member of a type, the Self type refers to that type. In the members of a class declaration, Self can appear as the return type of a method and in the body of a method, but not in any other context. For example, the code below shows an instance method f whose return type is Self.

 

위 예제의 마지막 부분은 Self가 변수 자체의 컴파일 타임 유형 슈퍼 클래스가 아니라 z 값의 런타임 유형 서브 클래스를 참조 함을 보여줍니다.

The last part of the example above shows that Self refers to the runtime type Subclass of the value of z, not the compile-time type Superclass of the variable itself.

 

중첩 형식 선언 내에서 Self 형식은 가장 안쪽 형식 선언에 의해 도입 된 형식을 나타냅니다.

Inside a nested type declaration, the Self type refers to the type introduced by the innermost type declaration.

 

Self 유형은 Swift 표준 라이브러리의 type (of :) 함수와 동일한 유형을 나타냅니다. 

현재 유형의 멤버에 액세스하기 위해 Self.someStaticMember를 작성하는 것은 쓰기 유형 (of : self) .someStaticMember와 동일합니다.

The Self type refers to the same type as the type(of:) function in the Swift standard library. Writing Self.someStaticMember to access a member of the current type is the same as writing type(of: self).someStaticMember.

 

 

SELF 유형의 문법

GRAMMAR OF A SELF TYPE

self-type  Self

 

 

 

 

 

 

 

참조 : 애플공식문서 the swift programming language