Can I put different types of objects in the same NSMutableArray?

馋奶兔 提交于 2020-01-12 06:52:09

问题


I have classes A and B.
Can I put these classes in the same NSMutableArray without problems in future?

Example:

NSMutableArray *maincoll = [[NSMutableArray alloc] init];
ClassA *ca = [[classA alloc] init];
ClassB *cb = [[classB alloc] init];
//here is case
[maincoll addObject:ca];
[maincoll addObject:cb];
...

回答1:


Yes. No limitations. The only thing you have to be careful of is when you retrieve items from the array to verify their class (if necessary) before starting to use them.




回答2:


Yes.

This may be the shortest answer I've ever posted.




回答3:


You can put objects of different classes. They just need to inherit from NSObject. Don't forget to release after adding to the array.



来源:https://stackoverflow.com/questions/7903172/can-i-put-different-types-of-objects-in-the-same-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!