迅捷:等效的objective-c运行时类 [英] swift: Equivalent objective-c runtime class
问题描述
什么是低于Objective-C代码的等效Swift代码.我找不到带有运行时概念的快速主题.
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];
试图获取self
的类对象?
更新:
尝试使用以下代码,错误为'UIViewController.type' doesn't conform to protocol 'AnyObject'
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.
推荐答案
首先,在不知道您在Objective-C中使用该类对象做什么的情况下,很难将该代码转换为Swift.
First, it's hard to translate that code to Swift without knowing what you used that class object for in Objective-C.
在Objective-C中,类对象是对象,类型Class
可以保存指向任何类对象的指针.但是,当将Objective-C API桥接到Swift时,类型Class
将在Swift中转换为AnyClass!
,其中AnyClass
被定义为AnyObject.Type
. Swift中的类型不是对象,因此不直接等同于Objective-C中的类对象.但是,如果您打算使用Swift中的Objective-C API,无论如何它都会被桥接以期望AnyClass
,因此您必须传递一个类型.您可以使用.dynamicType
获得任何表达式的类型.例如:
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
((如果您真的想以与Objective-C中相同的方式将类对象作为Swift对象而不是Swift类型来获取,则有一些
(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.)
但是,您对问题的描述揭示了另一个问题.如果只想获取对象的类型,而self
是一个对象,则var klass: AnyClass = object_getClass(self)
应该已经起作用,因为object_getClass()
接受AnyObject
并返回AnyClass
.唯一无法解释的解释是self
是 not 一个对象.您的错误消息表明,确实self
是类型,而不是对象.
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
是一种类型.您应该为代码提供真正的上下文(显然,您没有将Class class = [self class];
放在文件的顶层),因为从上下文中删除很容易造成误解.在Objective-C可可中,有两个名为class
的方法截然不同:实例方法-class
返回对象的类,而类方法+class
则返回对象的类.只需返回它被调用的(类)对象.由于您的代码在类方法中,因此在Objective-C中,self
指向类对象,而[self class]
运行类方法+class
,该方法仅返回被调用的对象.换句话说,[self class]
与self
完全相同.您应该一直都写过self
,但是没有意识到.
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.
所以答案是,Objective-C应该是
So the answer is that the Objective-C should have been
Class class = self;
和类似的Swift应该是
and similarly the Swift should be
var klass: AnyClass = self
这篇关于迅捷:等效的objective-c运行时类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!