ios swift:等效的objective-c 运行时类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24403718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:05:09  来源:igfitidea点击:

swift: Equivalent objective-c runtime class

iosswiftobjective-c-runtime

提问by Mani

What is equivalent swift code for below Objective-C code. I couldn't find swift topic with runtime concept.

以下 Objective-C 代码的等效 swift 代码是什么。我找不到带有运行时概念的 swift 主题。

#import <objc/runtime.h>    
Class class = [self class];

Trying to get class object of self?

试图获取 的类对象self

Update:Tried with below code, got error as 'UIViewController.type' doesn't conform to protocol 'AnyObject'

更新:尝试使用以下代码,出现错误'UIViewController.type' doesn't conform to protocol 'AnyObject'

var klass: AnyClass = object_getClass(self)

Note:Found this post,but wouldn't helped.

注意:找到了这篇文章,但没有帮助。

回答by newacct

First, it's hard to translate that code to Swift without knowing what you used that class object for in Objective-C.

首先,如果不知道您在 Objective-C 中使用该类对象的目的,就很难将该代码转换为 Swift。

In Objective-C, class objects are objects, and the type Classcan hold a pointer to any class object. However, when Objective-C APIs are bridged to Swift, the type Classis converted to AnyClass!in Swift, where AnyClassis 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 AnyClassanyway, so you have to pass a type. You can get the type of any expression using .dynamicType; for example:

在 Objective-C 中,类对象是对象,类型Class可以持有指向任何类对象的指针。但是,当 Objective-C API 被桥接到 Swift 时,类型Class会转换为AnyClass!Swift,其中AnyClass定义为AnyObject.Type. Swift 中的类型不是对象,因此不直接等同于 Objective-C 中的类对象。但是,如果您打算使用 Swift 的 Objective-C API,AnyClass无论如何它都会被桥接,因此您必须传递一个类型。您可以使用.dynamicType;获取任何表达式的类型。例如:

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 waysto do that too.)

(如果你真的想以与 Objective-C 中相同的方式将类对象作为 Swift 对象,而不是作为 Swift 类型,也有一些复杂的方法可以做到这一点。)

However, your description of your problem reveals another issue. If you just want to get the type of an object, and selfis an object, then var klass: AnyClass = object_getClass(self)should have worked, since object_getClass()takes an AnyObjectand returns an AnyClass. The only explanation for it not working is if selfis notan object. Your error message reveals that, indeed, selfis a type, not an object.

但是,您对问题的描述揭示了另一个问题。如果你只是想获取一个对象的类型,并且self是一个对象,那么var klass: AnyClass = object_getClass(self)应该可以工作,因为object_getClass()需要一个AnyObject并返回一个AnyClass. 它不工作,唯一的解释是,如果self没有一个对象。您的错误消息表明,确实selftype,而不是 object。

selfis 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 twovery 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, selfpoints 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 selfall along, but didn't realize it.

self如果此代码在类方法中运行,则是一种类型。您应该真正为您的代码提供上下文(显然,您没有放在Class class = [self class];文件的顶层),因为脱离上下文很容易误解。在Objective-C Cocoa 中,有两个非常不同的方法命名为class:一个实例方法,-class返回对象的类,和一个类方法,+class它只返回调用它的(类)对象。由于您的代码位于类方法中,因此在 Objective-C 中,self指向类对象并[self class]运行类方法+class,该方法只返回调用它的对象。换句话说,[self class]与 完全相同self。你应该刚刚写self一直以来,却没有意识到。

So the answer is that the Objective-C should have been

所以答案是 Objective-C 应该是

Class class = self;

and similarly the Swift should be

同样,Swift 应该是

var klass: AnyClass = self

回答by Dafydd Williams

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

在 Swift 3 中,self.dynamicType(以及dynamicType一般情况下)已被删除。

You now use:

你现在使用:

type(of: self)

回答by Michal Zaborowski

var klass: AnyClass = object_getClass(self)
NSStringFromClass(klass)