What is equivalent swift code for below Objective-C code. I couldn't find swift topic with runtime concept.
#import <objc/runtime.h> Class class = [self class];
Trying to get class object of self
?
Update: Tried with below code, got error as 'UIViewController.type' doesn't conform to protocol 'AnyObject'
var klass: AnyClass = object_getClass(self)
Note: Found this post, but wouldn't helped.
Even when written without a single line of Objective-C code, every Swift app executes inside the Objective-C runtime, opening up a world of dynamic dispatch and associated runtime manipulation.
Moreover, the concepts of Swift are similar to Objective-C. It includes late binding, dynamic dispatch, extensible programming, etc. Additionally, it works fine with figuring out the bugs and also addresses Null pointers. In the year 2015, Swift 2.0 was released to the market.
The Objective-C runtime is a runtime library that provides support for the dynamic properties of the Objective-C language, and as such is linked to by all Objective-C apps. Objective-C runtime library support functions are implemented in the shared library found at /usr/lib/libobjc.
Apple's official website claims that Swift is up to 2.6 times faster than Objective-C. They named the language “Swift” for a reason. Swift's simpler syntax and compile-time type checking help increase its performance. But the most significant boost in performance comes from its memory management and scalability.
First, it's hard to translate that code to Swift without knowing what you used that class object for in Objective-C.
In Objective-C, class objects are objects, and the type Class
can hold a pointer to any class object. However, when Objective-C APIs are bridged to Swift, the type Class
is converted to AnyClass!
in Swift, where AnyClass
is defined as AnyObject.Type
. Types in Swift are not objects, and thus are not directly equivalent to class objects in Objective-C. However, if you intend to use an Objective-C API from Swift, it will have been bridged to expect AnyClass
anyway, so you have to pass a type. You can get the type of any expression using .dynamicType
; for example:
self.dynamicType
(If you really want to get the class object as an Swift object the same way as in Objective-C, and not as a Swift type, there are some convoluted ways to do that too.)
However, your description of your problem reveals another issue. If you just want to get the type of an object, and self
is an object, then var klass: AnyClass = object_getClass(self)
should have worked, since object_getClass()
takes an AnyObject
and returns an AnyClass
. The only explanation for it not working is if self
is not an object. Your error message reveals that, indeed, self
is a type, not an object.
self
is a type if this code is running in a class method. You should have really given context for your code (obviously, you didn't put Class class = [self class];
at the top level of a file), because taken out of context it's easy to misunderstand. In Objective-C Cocoa, there are two very different methods named class
: an instance method, -class
, which returns the class of the object, and a class method, +class
, which simply returns the (class) object it's called on. Since your code is in a class method, in Objective-C, self
points to a class object, and [self class]
runs the class method +class
, which just returns the object it's called on. In other words, [self class]
is exactly identical to self
. You should have just written self
all along, but didn't realize it.
So the answer is that the Objective-C should have been
Class class = self;
and similarly the Swift should be
var klass: AnyClass = self
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