0

Is it possible to check if an object (not as in OOP) is of reference type or value type?

let something : Any = getSomething() // func getSomething() -> Any
let isReferenceType : Bool = // how to check?

EDIT: As pointed out this is practically a duplicate of Check if `Any` value is object

iur
  • 2,056
  • 2
  • 13
  • 30

1 Answers1

2

This is not so easy as it seems, see

let isReferenceType: Bool = type(of: something) is AnyClass

See How to test whether generic variable is of type AnyObject

However, if you need such things, then usually you have some big problem with your architecture that you should address instead. Using Any type should be the last resort for exceptional situations.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • True, I am in the process of writing a more specific question about where clauses for extending dictionary. – iur Feb 27 '19 at 14:08