[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.
要么
[yourObject isMemberOfClass:[a class]]
// Returns a Boolean value that indicates whether the receiver is an instance of a
// given class.
6 回答
要测试object是否是类a的实例:
要么
要获取对象的类名,可以使用
NSStringFromClass
函数:或来自objective-c运行时api的c函数:
EDIT: 在斯威夫特
你也可以使用
在任何NSObject上
Apple文档中有关 isKindOfClass 的含义
在类集群表示的对象上使用此方法时要小心 . 由于类集群的性质,您获得的对象可能并不总是您期望的类型 . 如果调用返回类集群的方法,则该方法返回的确切类型是您可以对该对象执行的操作的最佳指示 . 例如,如果方法返回指向NSArray对象的指针,则不应使用此方法来查看该数组是否可变,如以下代码所示:
如果在代码中使用此类构造,您可能认为修改实际上不应修改的对象是可以的 . 这样做可能会为期望对象保持不变的其他代码产生问题 .
如果您想检查特定的课程,那么您可以使用
如果你想获得 class 的名称,只需致电: -
您还可以检查运行时间 . 在代码和内部(lldb)控制台写入一个断点
像这样..