swift 类型系统 Self self Type

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.

猜你喜欢

转载自www.cnblogs.com/feng9exe/p/9182431.html