当我使用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 calling TheType.self. The syntax TheType.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 calling Int.Type. You would call Int.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 notation identifier: 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 write var 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 of T.Type (where T conforms to Decodable) is because any type conforming to Decodable has an initializer init(from decoder: Decoder). The decode method will need to call this init method on a type that conforms to Decodable, not on an instance of a type that conforms to Decodable. 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
发布评论