I feel necessary to quote from the Swift Programming Language documentation first of all:
Classes have additional capabilities that structures do not:
- Type casting enables you to check and interpret the type of a class instance at runtime.
According to this, it may be helpful for someone in the future:
func areTheySiblings(class1: AnyObject!, class2: AnyObject!) -> Bool {
return object_getClassName(class1) == object_getClassName(class2)
}
and the tests:
let myArray1: Array<AnyObject> = Array()
let myArray2: Array<Int> = Array()
let myDictionary: Dictionary<String, Int> = Dictionary()
let myString: String = String()
let arrayAndArray: Bool = self.areTheySiblings(myArray1, class2: myArray2) // true
let arrayAndString: Bool = self.areTheySiblings(myArray1, class2: myString) // false
let arrayAndDictionary: Bool = self.areTheySiblings(myArray1, class2: myDictionary) // false
UPDATE
you also can overload a new operator for doing such a thing, like e.g. this:
infix operator >!<
func >!< (object1: AnyObject!, object2: AnyObject!) -> Bool {
return (object_getClassName(object1) == object_getClassName(object2))
}
and the results:
println("Array vs Array: \(myArray1 >!< myArray2)") // true
println("Array vs. String: \(myArray1 >!< myString)") // false
println("Array vs. Dictionary: \(myArray1 >!< myDictionary)") // false
UPDATE#2
you can also use it for your own new Swift classes, like e.g. those:
class A { }
class B { }
let a1 = A(), a2 = A(), b = B()
println("a1 vs. a2: \(a1 >!< a2)") // true
println("a1 vs. b: \(a1 >!< b)") // false