Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift中的T.类型是什么_Swift_Generics_Types - Fatal编程技术网

swift中的T.类型是什么

swift中的T.类型是什么,swift,generics,types,Swift,Generics,Types,当我使用JSONDecoder().decode()时,谁能告诉我什么是T.Type 我认为这是一种对我编码的数据进行解码的类型 许多示例使用上述方法,如下所示: JSONEncoder().decode([People].self, from: data) 如果我检查该方法的定义,我可以看到T.Type 我知道泛型,但什么是T.Type 仅仅T和T.Type之间有什么区别 当我们声明一些变量时,我们像这样分配它们的类型 var someValue:Int ,而不是var someValue:

当我使用
JSONDecoder().decode()
时,谁能告诉我什么是
T.Type

我认为这是一种对我编码的数据进行解码的类型

许多示例使用上述方法,如下所示:

JSONEncoder().decode([People].self, from: data)
如果我检查该方法的定义,我可以看到
T.Type

我知道泛型,但什么是
T.Type

仅仅
T
T.Type
之间有什么区别

当我们声明一些变量时,我们像这样分配它们的类型

var someValue:Int
,而不是
var someValue:Int.self

什么是
T.Type
Type.self

  • T.Type
    在参数和约束中用于表示“事物本身的类型,而不是事物的实例”

    例如:

    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"
    }
    
    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"
    
  • 通过调用
    Type.self
    ,可以在运行时获得对类型的
    .Type
    (类型对象本身)的引用。语法
    Type.Type
    仅用于类型声明和类型签名中,以向编译器指示实例与类型的区别。例如,在运行时或在函数实现中调用
    Int.type
    ,实际上无法获取对
    Int
    类型的引用。您可以调用
    Int.self

  • 在示例代码
    var-someValue:Int
    中,特定符号
    identifier:Type
    (在本例中,
    someValue:Int
    )表示someValue将是Int的一个实例。如果希望someValue是对实际Int类型的引用,您可以编写
    var someValue:Int.Type=Int.self
    记住,
    .Type
    符号仅在向编译器声明类型和类型签名时使用,而
    .self
    属性在实际代码中用于在执行时检索对类型对象本身的引用

  • JSONDecoder().decode
    要求参数
    T.Type
    (其中
    T
    符合
    Decodable
    )的原因是符合
    Decodable
    的任何类型都有一个初始值设定项
    init(来自解码器:decoder)
    decode
    方法需要在符合
    Decodable
    的类型上调用此init方法,而不是在符合
    Decodable
    的类型实例上调用此init方法。例如:

    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"
    }
    
    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
T
的最新版本。感谢您提供的详细信息和极好的回答!通过谷歌搜索“Swift Metatype”可以找到相关的苹果文档,这应该会让你找到:。极好的答案很好的答案!!非常感谢。