如何测试Objective-C中的对象?

时间:2021-09-02 08:39:49

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类,我该怎么做呢?

6 个解决方案

#1


336  

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 NSStringFromClass function:

要获取对象的类名,可以使用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
}

#2


19  

You also can use

你也可以使用

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

on any NSObject

在任何NSObject

#3


3  

What means about isKindOfClass in Apple Documentation

关于苹果文档中的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.

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

#4


3  

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

如果您想要检查特定的类,那么您可以使用

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

#5


1  

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);

#6


0  

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

您还可以检查运行时间。在代码和内部(lldb)控制台写入一个断点。

(lldb) po [yourObject class]

Like this..

像这样的. .

如何测试Objective-C中的对象?

#1


336  

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 NSStringFromClass function:

要获取对象的类名,可以使用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
}

#2


19  

You also can use

你也可以使用

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

on any NSObject

在任何NSObject

#3


3  

What means about isKindOfClass in Apple Documentation

关于苹果文档中的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.

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

#4


3  

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

如果您想要检查特定的类,那么您可以使用

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

#5


1  

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);

#6


0  

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

您还可以检查运行时间。在代码和内部(lldb)控制台写入一个断点。

(lldb) po [yourObject class]

Like this..

像这样的. .

如何测试Objective-C中的对象?