简体   繁体   English

如何在Swift中获取变量“ Any?”的实型

[英]How can I get a “Any?” variable's real type in Swift

I get property list from Mirror.children, it returns (label: String?, value: Any). 我从Mirror.children获取属性列表,它返回(标签:String ?,值:Any)。 How can i get every value's real type. 我如何获取每个值的实型。

var a: Int = 1
var b: Int? = nil
var c: Any?

c = a
getType(c) -> "Int"

c = b
getType(c) -> "Optional<Int>"

您可以看一下c.dynamicType

Metatype Type 元类型类型

A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types. 元类型类型是指任何类型的类型,包括类类型,结构类型,枚举类型和协议类型。

The metatype of a class, structure, or enumeration type is the name of that type followed by .Type. 类,结构或枚举类型的元类型是该类型的名称,后跟.Type。 The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the name of that protocol followed by .Protocol. 协议类型的元类型(不是在运行时符合协议的具体类型)是该协议的名称,后跟.Protocol。 For example, the metatype of the class type SomeClass is SomeClass.Type and the metatype of the protocol SomeProtocol is SomeProtocol.Protocol. 例如,类类型SomeClass的元类型是SomeClass.Type,协议SomeProtocol的元类型是SomeProtocol.Protocol。

You can use the postfix self expression to access a type as a value. 您可以使用后缀自我表达式来访问类型作为值。 For example, SomeClass.self returns SomeClass itself, not an instance of SomeClass. 例如,SomeClass.self返回自身的SomeClass,而不是SomeClass的实例。 And SomeProtocol.self returns SomeProtocol itself, not an instance of a type that conforms to SomeProtocol at runtime. 而且SomeProtocol.self返回自身的SomeProtocol,而不是在运行时符合SomeProtocol的类型的实例。 You can use a dynamicType expression with an instance of a type to access that instance's dynamic, runtime type as a value, as the following example shows: 可以将dynamicType表达式与类型的实例一起使用,以访问该实例的动态运行时类型作为值,如以下示例所示:

class SomeBaseClass {
    class func printClassName() {
        print("SomeBaseClass")
    }
}
class SomeSubClass: SomeBaseClass {
    override class func printClassName() {
        print("SomeSubClass")
    }
}
let someInstance: SomeBaseClass = SomeSubClass()
// The compile-time type of someInstance is SomeBaseClass,
// and the runtime type of someInstance is SomeBaseClass
someInstance.dynamicType.printClassName()
// prints "SomeSubClass

Use the identity operators (=== and !==) to test whether an instance's runtime type is the same as its compile-time type. 使用标识运算符(===和!==)来测试实例的运行时类型是否与其编译时类型相同。

if someInstance.dynamicType === someInstance.self {
    print("The dynamic type of someInstance is SomeBaseCass")
} else {
    print("The dynamic type of someInstance isn't SomeBaseClass")
}
// prints "The dynamic type of someInstance isn't SomeBaseClass"

Use an initializer expression to construct an instance of a type from that type's metatype value. 使用初始化程序表达式根据该类型的元类型值构造该类型的实例。 For class instances, the initializer that's called must be marked with the required keyword or the entire class marked with the final keyword. 对于类实例,被调用的初始化器必须标记为required关键字,或者整个类标记为final关键字。

class AnotherSubClass: SomeBaseClass {
    let string: String
    required init(string: String) {
        self.string = string
    }
    override class func printClassName() {
        print("AnotherSubClass")
    }
}
let metatype: AnotherSubClass.Type = AnotherSubClass.self
let anotherInstance = metatype.init(string: "some string")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift 4中,如何获取存储在“ Any”类型变量中的数据类型的字符串表示形式? - In Swift 4, how can you get the string-representation of a data type stored in a variable of type 'Any'? 如何在 Swift 中不使用任何其他 Swift 类型的情况下构建自定义类型? - How I can build a custom Type without using any other Swift Type in Swift? 将变量分配给 function 中的类型,我怎样才能让类型本身成为描述变量 output 类型的输入? : Swift 5 - Assigning variable to a type in a function, how can I have the type itself be an input that will describe the type of a variable output? : Swift 5 如何在swift3中从Any获取字符串 - How can I get String from Any in swift3 如何在Swift中检查实例的类型? - How can I check an instance's type in Swift? 在Swift中,如何获取`Any`变量的真实大小? - In Swift, how to get the true size of an `Any` variable? 如何访问存储在“ Any”类型变量中的值 - How can I access values stored in a variable of type “Any” 如何从 Swift 中的 DNS 查询中获取真实 IP 地址? - How can I get a real IP address from DNS query in Swift? 如何获取Swift类以实时更新ViewController中的标签? - How can I get my Swift class to update the label in my viewcontroller in real time? 如何在Swift中获取初始的闭包变量? - How can I get initial closure variable in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM