Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Objective-C, how do I test the object type?

Tags:

objective-c

I need to test whether the object is of type NSString or UIImageView. How can I accomplish this? Is there some type of "isoftype" method?

like image 816
James Skidmore Avatar asked Jul 17 '09 17:07

James Skidmore


People also ask

How do I find the class of an object in Objective C?

[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.

Is kind of 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. Required.

How do you declare an object in Objective C?

Creating a new object in Objective-C is usually a two-step process. First, memory has to be allocated for the object, then the object is initialized with proper values. The first step is accomplished through the alloc class method, inherited from NSObject and rarely, if ever, overridden.


1 Answers

If your object is myObject, and you want to test to see if it is an NSString, the code would be:

[myObject isKindOfClass:[NSString class]] 

Likewise, if you wanted to test myObject for a UIImageView:

[myObject isKindOfClass:[UIImageView class]] 
like image 72
mmc Avatar answered Sep 22 '22 16:09

mmc