Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for the class type

When I send an object to NSLog, I get a string with three attributes. First is the class of the object, second is the frame of the object, and third is the CALayer of the object. The second and third attributes are titled (e.g. layer = ), so I can call them by the title (e.g. myObject.layer).

The first one isn't. How do I test for the class type?

Thanks!

like image 750
Eric Christensen Avatar asked Nov 30 '22 12:11

Eric Christensen


2 Answers

To get the class of an object, simply call [myObject class]. You can compare this to a desired class as follows:

if ([myObject class] == [SomeClass class]) { /* ... */ }
if ([myObject class] == [NSString class]) { /* ... */ }

If you are simply looking for the class name as a string, you can use the NSStringFromClass function as follows:

NSString * className = NSStringFromClass([myObject class]);
like image 56
e.James Avatar answered Dec 05 '22 02:12

e.James


If you also want to include subclasses of targeted class use:

[object isKindOfClass:[SomeClass class]]
like image 24
diederikh Avatar answered Dec 05 '22 02:12

diederikh