Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

ios - swift: Equivalent objective-c runtime class

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...