I cannot seem to find a function or method that returns the type of an object in Swift.
How does one retrieve the type or class of an object in Swift?
I tried using Obj-C classes which obviously did not work.
In python you have things like type
or isinstance
In Javascript you have constructor
As mentioned below, the value type instance keeps a unique copy of its data. So when using = syntax, it will pass all values to new object. Then the change in new object will not affect to old object.
A protocol that objects adopt to provide functional copies of themselves.
The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.1+ tvOS 9.0+ watchOS 2.0+
If you just need to check an object's type, you can use the type check operator, is
, as in:
if myObject is UIView {
// do something
}
If you want to try to call a method on the object but you aren't sure of the class, downcast the object to the type you need, like this:
if let myView = myObject as? UIView {
myView.layer.backgroundColor = myColor
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With