ios 用 Swift 比较类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27849587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:16:53  来源:igfitidea点击:

Comparing types with Swift

iosswift

提问by puttputt

I'm looking to do something like the following, but when I try to see if b == Test.self I get the error "Any class is not convertible to MirrorDisposition". How can I checked to see if a Type is equal to another type?

我正在做类似下面的事情,但是当我尝试查看 b == Test.self 时,我收到错误“任何类都不能转换为 MirrorDisposition”。如何检查一个类型是否等于另一个类型?

class Test {

}

var a = Test.self

var b : AnyClass = a

if(b == Test.self) {
    println("yes")
}
else {
    println("no")
}

回答by Martin R

Use the "identical to" operator ===:

使用“等同于”运算符===

if b === Test.self {
    print("yes")
}
else {
    print("no")
}

This works because the type of a class it itself a class object and can therefore be compared with ===.

这是有效的,因为类的类型本身就是一个类对象,因此可以与===.

It won't work with structs. Perhaps someone has a better answer that works for all Swift types.

它不适用于structs。也许有人有一个更好的答案,适用于所有 Swift 类型。

回答by Long Pham

if b.isKindOfClass(Test) {
    println("yes")
} else {
    println("no")
}

Edit: Swift 3

编辑:斯威夫特 3

if b.isKind(of: Test.self) {
    print("yes")
} else {
    print("no")
}

try it :)

尝试一下 :)

回答by Xiaojun

If you just want to compare the class types, then you can simply use NSStringFromClass to compare the class names as below:

如果您只想比较类类型,那么您可以简单地使用 NSStringFromClass 来比较类名,如下所示:

class Test {}

var a = Test.self
var b : AnyClass = a

if(NSStringFromClass(b) == NSStringFromClass(Test.self)) {
    println("yes")
} else {
    println("no")
}

If you want to find out or compare the type of an object, you can use "if ... is ... {}" syntax as code below:

如果你想找出或比较一个对象的类型,你可以使用“if ... is ... {}”语法作为下面的代码:

class Test { }
class Testb { }

var a = Test.self
let b : AnyObject = Testb()

if(b is Test) {
    println("yes")
} else {
    println("no")
}

If you want to do object to object equality check with == operator, you can make your Test class conforms to Equatable protocol. This can be extended to both Struct and Class types in Swift as explained in this NSHipster article: http://nshipster.com/swift-comparison-protocols/.

如果您想使用 == 运算符进行对象到对象的相等性检查,您可以使您的 Test 类符合 Equatable 协议。这可以扩展到 Swift 中的 Struct 和 Class 类型,如这篇 NSHipster 文章中所述:http://nshipster.com/swift-comparison-protocols/ 。

You code then can be written as below, please note: this is object equality checking, so you cannot define b as AnyClass, you need to instead define as AnyObject.

然后你的代码可以写成如下,请注意:这是对象相等性检查,所以你不能定义b为AnyClass,你需要定义为AnyObject。

class Test: Equatable { }

// MARK: Equatable
func ==(lhs: Test, rhs: Test) -> Bool {
    return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}

var a = Test()
var b : AnyObject = a

if((b as Test) == a) {
    println("yes")
} else {
    println("no")
}