How to use id variable in objective-c?

I have an NSArray that contains two types of objects. Lets call them Apple and Orange.

Apple *myApple = [self.searchQueryResults objectAtIndex:indexPath.row];

When I build my cell, I do not know what type in my array is apples or oranges. How can I use a generic identifier type to store an object and then cast accordingly?

+3
source share
2 answers

You can use isKindOfClass:(Class)aClassto check the type of a class:

if ([myObject isKindOfClass:[Apple class]])
   // do stuff

, , . NSArray , , , , . , -

Apple *myApple = (Apple *)myObject;

, , , , , , , , - .

+2

NSArray.

, , , (, Fruit). , , -, , , , .

, , , - - - , - . , BOOM!

+2

Source: https://habr.com/ru/post/1759624/


All Articles