什么是T.Type in swift

编程入门 行业动态 更新时间:2024-10-27 16:33:23
本文介绍了什么是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 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

本文发布于:2023-11-05 16:02:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Type   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!