如何测试对象在 Objective-C 中的哪个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055940/
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-09-03 22:45:49  来源:igfitidea点击:

How do I test which class an object is in Objective-C?

objective-c

提问by futureelite7

How do I test whether an object is an instance of a particular class in Objective-C? Let's say I want to see if object a is an instance of class b, or class c, how do I go about doing it?

如何测试对象是否是 Objective-C 中特定类的实例?假设我想看看对象 a 是 b 类还是 c 类的实例,我该怎么做?

回答by Vladimir

To test if object is an instance of class a:

要测试对象是否是类 a 的实例:

[yourObject isKindOfClass:[a class]]
// Returns a Boolean value that indicates whether the receiver is an instance of 
// given class or an instance of any class that inherits from that class.

or

或者

[yourObject isMemberOfClass:[a class]]
// Returns a Boolean value that indicates whether the receiver is an instance of a 
// given class.

To get object's class name you can use NSStringFromClassfunction:

要获取对象的类名,您可以使用NSStringFromClass函数:

NSString *className = NSStringFromClass([yourObject class]);

or c-function from objective-c runtime api:

或来自objective-c运行时api的c函数:

#import <objc/runtime.h>

/* ... */

const char* className = class_getName([yourObject class]);
NSLog(@"yourObject is a: %s", className);

EDIT:In Swift

编辑:在斯威夫特

if touch.view is UIPickerView {
    // touch.view is of type UIPickerView
}

回答by Clement M

You also can use

你也可以使用

NSString *className = [[myObject class] description]; 

on any NSObject

在任何 NSObject 上

回答by Duke

What means about isKindOfClassin Apple Documentation

Apple 文档中isKindOfClass是什么意思

Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:

在由类簇表示的对象上使用此方法时要小心。由于类簇的性质,您返回的对象可能并不总是您期望的类型。如果调用返回类簇的方法,则该方法返回的确切类型是您可以对该对象执行的操作的最佳指标。例如,如果某个方法返回一个指向 NSArray 对象的指针,则不应使用此方法来查看数组是否可变,如下代码所示:

// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
    // Modify the object
}

If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

如果您在代码中使用这样的结构,您可能认为修改一个实际上不应该修改的对象是可以的。这样做可能会给其他期望对象保持不变的代码带来问题。

回答by Inder Kumar Rathore

If you want to check for a specific class then you can use

如果你想检查一个特定的类,那么你可以使用

if([MyClass class] == [myClassObj class]) {
//your object is instance of MyClass
}

回答by tryKuldeepTanwar

if you want to get the name of the class simply call:-

如果你想获得类的名称,只需调用:-

id yourObject= [AnotherClass returningObject];

NSString *className=[yourObject className];

NSLog(@"Class name is : %@",className);

回答by Saranjith

You can also check run time. Put one breakpoint in code and inside (lldb) console write

您还可以检查运行时间。在代码中放置一个断点并在(lldb)控制台写入

(lldb) po [yourObject class]

Like this..

像这样..

enter image description here

在此处输入图片说明