什么是T.Type in swift [英] what is T.Type in swift
问题描述
当我使用JSONDecoder().decode()时,谁能告诉我T.Type
是什么?
Can anyone tell me what is T.Type
when i use JSONDecoder().decode()?
我认为对我编码的数据进行解码是一种类型
I think it is type to decode data what i encoded
很多例子都使用上述方法
So many example use above method like this
JSONEncoder().decode([People].self, from: data) ...
如果我检查该方法的定义,我可以看到T.Type
.
If i check that method's definition i can see T.Type
.
我知道泛型,但是什么是T.Type
I know the generics but what is T.Type
T和T.Type有什么区别
What's the difference between just T and T.Type
当我们声明一些变量时,我们会像这样分配它们的类型
when we declare some variables, we allocated their types like this
var someValue: Int
,不是var someValue: Int.self
什么是T.Type
和Type.self
推荐答案
-
在参数和约束中使用
-
T.Type
表示事物本身的类型,而不是事物的实例".T.Type
is used in parameters and constraints to mean "the type of the thing itself, not an instance of the thing".例如:
class Example { static var staticVar: String { return "Foo" } var instanceVar: String { return "Bar" } } func printVar(from example: Example) { print(example.instanceVar) // "Bar" print(example.staticVar) // Doesn't compile, _Instances_ of Example don't have the property "staticVar" } func printVar(from example: Example.Type) { print(example.instanceVar) // Doesn't compile, the _Type_ Example doesn't have the property "instanceVar" print(example.staticVar) // prints "Foo" }
-
您可以通过调用
TheType.self
在运行时获取对类型.Type
(类型对象本身) 的引用.语法TheType.Type
用于类型声明和类型签名,仅用于向编译器指示实例与类型的区别.实际上,您无法在运行时或在函数实现中通过调用Int.Type
来引用Int
的类型.您会打电话给Int.self
You get a reference to a Type's
.Type
(the Type object itself) at runtime by callingTheType.self
. The syntaxTheType.Type
is used in type declarations and type signatures only to indicate to the compiler the instance vs. type distinction. You can't actually get a reference to, for example,Int
's type at runtime or in your function implementations by callingInt.Type
. You would callInt.self
在示例代码
var someValue: Int
中,特定表示法identifier: Type
(在本例中为someValue: Int
)表示,其中someValue将是 instance 的.如果要让someValue引用实际的Int类型,请编写var someValue: Int.Type = Int.self
.请记住,.Type
表示法仅在向编译器声明类型和类型签名时使用,而.self
属性在实际中使用.代码以在执行时检索对类型对象本身的引用.In the example code
var someValue: Int
, the specific notationidentifier: Type
(in this case,someValue: Int
) means that someValue will be an instance of Int. If you wanted someValue to be a reference to the actual type Int, you would writevar someValue: Int.Type = Int.self
Remember that the.Type
notation is only used when declaring types and type signatures to the compiler, and the.self
property is used in actual code to retrieve a reference to the type object itself at execution time.JSONDecoder().decode
需要参数T.Type
(其中T
符合Decodable
的原因)的原因是,任何符合Decodable
的 type 都具有初始化程序init(from decoder: Decoder)
.decode
方法将需要在符合Decodable
的类型上调用此init方法,而不是在符合Decodable
的类型的 instance 上调用此init方法.例如:The reason why
JSONDecoder().decode
requires a parameter ofT.Type
(whereT
conforms toDecodable
) is because any type conforming toDecodable
has an initializerinit(from decoder: Decoder)
. Thedecode
method will need to call this init method on a type that conforms toDecodable
, not on an instance of a type that conforms toDecodable
. For example:var someString: String = "" someString.init(describing: 5) // Not possible, doesn't compile. Can't call an initializer on an _instance_ of String var someStringType: String.Type = String.self someStringType.init(describing: 5) // Iniitializes a String instance "5"
这篇关于什么是T.Type in swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!