简体   繁体   English

如何判断Swift中实例变量的类是什么

[英]How can I tell what the class of an instance variable is in Swift

Is there any easy way to tell what class an instance variable is in a Swift? 有没有简单的方法来告诉Swift中的实例变量是什么class In the JVM-based languages that I'm used to, you can do something like println(value.class) to get it's class. 在我习惯的基于JVM的语言中,您可以执行类似println(value.class)来获取它的类。

Is there something equivalent in Swift? 在Swift中有相同的东西吗?

The closest thing I can find in the docs is the ability to do "type checking" with the is <Class> keyword but that only helps me guess a little bit. 我在文档中找到的最接近的东西是能够使用is <Class>关键字进行“类型检查”,但这只能帮我猜一点。

I've run into a few situations in playing around where I thought I had one type of class, but actually had another and didn't know how to know for sure. 我遇到过一些情况 ,我认为我有一种类型,但实际上有另一种,并且不知道如何确定。

Use type.self to return a type that can be passed into a method that accepts a type-level argument. 使用type.self返回可以传递给接受类型级参数的方法的类型。 For example, UILabel.self can be passed to the isKindOfClass method call. 例如, UILabel.self可以传递给isKindOfClass方法调用。 The string representation of the class can be found via dynamicType.description() : 可以通过dynamicType.description()找到该类的字符串表示形式:

var label = UILabel()
println(label.dynamicType.description())
println(label.isKindOfClass(UILabel.self))

Swift-3 斯威夫特-3

var label = UILabel()
println(type(of: label).description())

Output 产量
UILabel 的UILabel
true 真正

Here's a bit more background -- there are two expressions to be aware of: the postfix self expression and the dynamic type expression. 这里有一些背景知识 - 有两个表达式需要注意:后缀自我表达和动态类型表达式。 From the docs : 来自文档

Postfix Self 后缀自我
A postfix self expression consists of an expression or the name of a type, immediately followed by .self. 后缀自我表达由表达式或类型名称组成,紧接着是.self。 It has the following forms: 它有以下形式:

 expression.self type.self 

The first form evaluates to the value of the expression. 第一个表单计算表达式的值。 For example, x.self evaluates to x. 例如,x.self计算为x。

The second form evaluates to the value of the type . 第二种形式评估类型的值 Use this form to access a type as a value. 使用此表单可以将类型作为值进行访问。 For example, because SomeClass.self evaluates to the SomeClass type itself, you can pass it to a function or method that accepts a type-level argument 例如,因为SomeClass.self计算SomeClass类型本身,您可以将它传递给接受类型级参数的函数或方法



Dyamic Type Expression 动态类型表达
A dynamicType expression consists of an expression, immediately followed by .dynamicType. dynamicType表达式由一个表达式组成,后面跟着.dynamicType。 It has the following form: 它具有以下形式:

 expression.dynamicType 

The expression can't be the name of a type. 表达式不能是类型的名称。 The entire dynamicType expression evaluates to the value of the runtime type of the expression. 整个dynamicType表达式求值为表达式的运行时类型的值。

As of beta 6 _stdlib_getTypeName gets the mangled type name of a variable. 从beta 6开始, _stdlib_getTypeName获取变量的_stdlib_getTypeName类型名称。 Paste this into an empty playground: 将其粘贴到空的操场上:

import Foundation

class PureSwiftClass {
}

var myvar0 = NSString() // Objective-C class
var myvar1 = PureSwiftClass()
var myvar2 = 42
var myvar3 = "Hans"

println( "TypeName0 = \(_stdlib_getTypeName(myvar0))")
println( "TypeName1 = \(_stdlib_getTypeName(myvar1))")
println( "TypeName2 = \(_stdlib_getTypeName(myvar2))")
println( "TypeName3 = \(_stdlib_getTypeName(myvar3))")

The output is: 输出是:

TypeName0 = NSString
TypeName1 = _TtC13__lldb_expr_014PureSwiftClass
TypeName2 = _TtSi
TypeName3 = _TtSS

Ewan Swick's blog entry helps to decipher these strings: Ewan Swick的博客文章有助于破译这些字符串:

eg _TtSi stands for Swift's internal Int type. 例如_TtSi代表Swift的内部Int类型。

Mike Ash has a great blog entry covering the same topic . Mike Ash有一篇很棒的博客文章,涵盖了相同的主题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM