swift: Equivalent objective-c runtime class
Asked Answered
D

3

36

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.

Dauphine answered 25/6, 2014 at 8:27 Comment(3)
please check this answer: #24162122Saxhorn
class is a reserved keyword in Swift. Most functionality can be use from the object itself. AnyObject has more functionality than NSObject. For instance the version property is now accessible from the object instead of first getting the class and then the version. So what do you want to do with it?Petitionary
It's not duplicate with that. Reader must read question carefully.Dauphine
B
49

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
Bugle answered 25/6, 2014 at 18:32 Comment(0)
E
12

In Swift 3, self.dynamicType (and dynamicType in general) has been removed.

You now use:

type(of: self)
Evan answered 5/5, 2017 at 1:44 Comment(0)
P
7
var klass: AnyClass = object_getClass(self)
NSStringFromClass(klass)
Pentangular answered 25/6, 2014 at 8:47 Comment(6)
Is it return class? or NSString?Dauphine
There is not Class type in Swift, only AnyObject, so you can get only class name (typealias AnyClass = AnyObject.Type)Pentangular
K I accept with this statement There is not Class type in Swift, But above answer is not correct even this statement corect.Dauphine
object_getClass(self)Pentangular
Did you try that? I got error as 'UIViewController.type' doesn't conform to protocol 'AnyObject'Dauphine
I've written this code in extension of class, and method also class method.Dauphine

© 2022 - 2024 — McMap. All rights reserved.