问题
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