I got stuck by the question in swift. Suppose I have one object, how to check whether it is from struct or class in swift.
-
Looks like it's impossible to do in Swift 3. However class object have **superclass** property and struct objects haven't. – Igor Kulagin Oct 31 '16 at 14:31
6 Answers
In Swift 3.0, you can call Mirror(reflecting:x).displayStyle
where x
is your value of interest. The result will be class
, struct
, enum
, dictionary
, set
... see the documentation https://developer.apple.com/reference/swift/mirror.displaystyle
Code sample:
struct SomeStruct {
var name: String
init(name: String) {
self.name = name
}
}
var astruct = SomeStruct(name:"myname")
Mirror(reflecting:astruct).displayStyle == .struct // will be true
Mirror(reflecting:astruct).displayStyle == .class; // will be false
class MyClass {
var name:String
init(name: String) {
self.name=name
}
}
var aclass = MyClass(name:"fsdfd")
Mirror(reflecting:aclass).displayStyle == .struct // will be false
Mirror(reflecting:aclass).displayStyle == .class // will be true
Of course, it would be best handled using a switch-case statement in practice.

- 3,470
- 2
- 25
- 23
This approach has been working for me in Swift 3:
class TestClass { }
struct TestStruct { }
var mystery:Any
mystery = TestClass()
// Is mystery instance a class type?
print(type(of:mystery) is AnyClass ? "YES" : "NO") // prints: "YES"
mystery = TestStruct()
// Is mystery instance a class type?
print(type(of:mystery) is AnyClass ? "YES" : "NO") // prints: "NO"
Note that this approach tells you only if an instance is a class type or not. The fact that it's not a class doesn't necessarily mean it's a struct (could be an enum, closure, tuple, etc.) But for most purposes and contexts this is enough to know if you are dealing with a reference type or a value type, which is usually what is needed.

- 13,457
- 4
- 41
- 37
-
Only if you know it is not a closure, because they are reference types too. – Binarian Jan 14 '18 at 08:48
There is is
operator.
if someInstance is SomeType {
// do something
}
And there is as?
operator.
if let someInstance = someInstance as? SomeType {
// now someInstance is SomeType
}

- 15,254
- 10
- 48
- 57
In swift4, checking class or struct
class TClass {}
struct TStruct {}
func who(_ any: Any) -> String {
if Mirror(reflecting: any).displayStyle == .class {
return "Class"
} else {
return "Struct"
}
}
print(who("Hello")) // Struct
print(who(TClass())) // Class
print(who(TStruct())) // Struct
print(who(1)) // Struct

- 4,549
- 3
- 20
- 20
You can do this by below given way and for more information on this please follow this link.
class Shape {
class func className() -> String {
return "Shape"
}
}
class Square: Shape {
override class func className() -> String {
return "Square"
}
}
class Circle: Shape {
override class func className() -> String {
return "Circle"
}
}
func getShape() -> Shape {
return Square() // hardcoded for example
}
let newShape: Shape = getShape()
newShape is Square // true
newShape is Circle // false
newShape.dynamicType.className() // "Square"
newShape.dynamicType.className() == Square.className()

- 1
- 1

- 12,428
- 8
- 29
- 52
A simple example for this:
var name = "Big Hero"
if name.isKindOfClass(NSString){
println("this is this class")
}else{
println("this is not this class")
}