1. 程式人生 > >swift 類型系統 Self self Type

swift 類型系統 Self self Type

關聯 flex ret dcl represent concrete req value part

namedClass:靜態類型;與類型實現直接關聯;可以用於初始化、類型檢查等。

namedClass.self:@thick,脫敏(脫關)類型;動態類型;可以作為元類型的實例;可以作為類型參量進行傳遞;可以用於繼承體系;

使用脫敏類型進行初始化時,需要與具體類型進行綁定。

namedClass.Type:元類型;用於脫敏類型聲明;脫敏類型類型檢查。

動態類型:編譯時不確定類型指定。

Self:動態時的具體類型。

Self:In that context, Self refers to the eventual type that conforms to the protocol.

代表具體的實際類型;動態類型。

https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

"Self" is a placeholder used in two different cases:

1. In a protocol, it refers to the type that conforms to the protocol in any particular use. In Equatable, for example, it‘s used to require that the two values being compared are of the same type. It‘s something like a generic type parameter that you don‘t have to put between the <…> because it‘s deduced from the context of its use.

2. In a class/static method, it can be used as the return type, to indicate that the return type is the type of the class to which the method was sent, rather than the class in which the method is declared. It‘s similar to ‘instancetype‘ in Obj-C.

AnyObject:

an untyped object

/// The flexible behavior of the `AnyObject` protocol is similar to

/// Objective-C‘s `id` type. For this reason, imported Objective-C types

/// frequently use `AnyObject` as the type for properties, method parameters,

/// and return values.

///

/// Casting AnyObject Instances to a Known Type

/// ===========================================

///

/// Objects with a concrete type of `AnyObject` maintain a specific dynamic

/// type and can be cast to that type using one of the type-cast operators

/// (`as`, `as?`, or `as!`).

Swift provides two special types for working with nonspecific types:

  • Any can represent an instance of any type at all, including function types.
  • AnyObject can represent an instance of any class type.

swift 類型系統 Self self Type