How can we check if a parameter passed in a function is a value or a reference type? For example
func isReferenceType(toTest: Any) {
return true // or fa
AnyObject
is a protocol that any class type automatically conforms to, so you can write:
func isReferenceType(toTest: Any) -> Bool {
return toTest.dynamicType is AnyObject
}
class Foo { }
struct Bar { }
isReferenceType(Foo()) // true
isReferenceType(Bar()) // false
isReferenceType("foo") // false
isReferenceType(123) // false
isReferenceType([1,2,3]) // false
Swift 5
func isReferenceType(_ toTest: Any) -> Bool {
return type(of: toTest) is AnyClass
}