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.
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.
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.
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
}
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
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()
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")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.