如何在objective-c中使用id变量?

时间:2022-09-07 08:26:32

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

我有一个NSArray,它包含两种类型的对象。我们叫他们苹果和橘子吧。

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

When I am building my cell's I don't know what type is in my array, apples or oranges. How can I use the generic id type to store the object, and then cast appropriately?

当我构建单元格时,我不知道数组中是什么类型,苹果还是橘子。如何使用通用id类型存储对象,然后适当地进行转换?

2 个解决方案

#1


2  

You can use isKindOfClass:(Class)aClass to test the class type:

您可以使用isKindOfClass:(Class)aClass来测试类类型:

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

I'm not sure why you think you have to cast though. NSArray doesn't care what type of object you store in it, so when you pull it out, it'll still be the same object. But if you want you could still cast by doing something like

我不知道你为什么认为你必须投。NSArray并不关心存储在其中的对象类型,所以当你取出它时,它仍然是相同的对象。但如果你愿意,你仍然可以做一些类似的事情

Apple *myApple = (Apple *)myObject;

Anyways, knowing the type of class it is should be enough for you to take appropriate action, since it's my understanding that you're showing both types in the same table, all you really need to do is appropriately display their different properties.

不管怎样,知道类的类型就足以让您采取适当的行动,因为我的理解是您在同一个表中显示这两种类型,您真正需要做的就是适当地显示它们的不同属性。

#2


2  

Don't mix types of classes in an NSArray.

不要在NSArray中混合类。

You can do it, you can run checks for the type - but it's a really bad idea, UNLESS they are both part of the same subclass tree (say, both derivatives of Fruit). Then at least something looking in there can assume what kind of Fruit it might be, and check for particulars.

你可以这样做,你可以运行类型检查——但这是一个非常糟糕的主意,除非它们都是同一个子类树的一部分(比如,水果的两个衍生物)。然后至少有东西在里面看可以假设它是什么水果,并检查细节。

Basically you will save yourself a lot of headaches if you don't mix types in container classes, when somewhere down the road some bit of code figures for some reason there are only Apples in there but someone throws in a Pear. Then, BOOM!

基本上,如果不在容器类中混合类型,就可以避免很多麻烦,因为在将来的某个时候,由于某些原因,代码中只有苹果,但是有人会抛出一个梨。然后,轰!

#1


2  

You can use isKindOfClass:(Class)aClass to test the class type:

您可以使用isKindOfClass:(Class)aClass来测试类类型:

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

I'm not sure why you think you have to cast though. NSArray doesn't care what type of object you store in it, so when you pull it out, it'll still be the same object. But if you want you could still cast by doing something like

我不知道你为什么认为你必须投。NSArray并不关心存储在其中的对象类型,所以当你取出它时,它仍然是相同的对象。但如果你愿意,你仍然可以做一些类似的事情

Apple *myApple = (Apple *)myObject;

Anyways, knowing the type of class it is should be enough for you to take appropriate action, since it's my understanding that you're showing both types in the same table, all you really need to do is appropriately display their different properties.

不管怎样,知道类的类型就足以让您采取适当的行动,因为我的理解是您在同一个表中显示这两种类型,您真正需要做的就是适当地显示它们的不同属性。

#2


2  

Don't mix types of classes in an NSArray.

不要在NSArray中混合类。

You can do it, you can run checks for the type - but it's a really bad idea, UNLESS they are both part of the same subclass tree (say, both derivatives of Fruit). Then at least something looking in there can assume what kind of Fruit it might be, and check for particulars.

你可以这样做,你可以运行类型检查——但这是一个非常糟糕的主意,除非它们都是同一个子类树的一部分(比如,水果的两个衍生物)。然后至少有东西在里面看可以假设它是什么水果,并检查细节。

Basically you will save yourself a lot of headaches if you don't mix types in container classes, when somewhere down the road some bit of code figures for some reason there are only Apples in there but someone throws in a Pear. Then, BOOM!

基本上,如果不在容器类中混合类型,就可以避免很多麻烦,因为在将来的某个时候,由于某些原因,代码中只有苹果,但是有人会抛出一个梨。然后,轰!