I was debugging something in my code and realized that the following compiles
NSMutableSet *s = [[NSMutableArray alloc] init];
Card *c = [[Card alloc] init];
[s addObject:c];
[s addObject:c];
[s addObject:c];
NSLog(@"Set now contains %d cards ", [s count]); //Prints 3
But the declared type is a Set here .. why are duplicates allowed? Why does this compiles at all?
[[NSMutableArray alloc] init]returns typeid, this is standard for Objective-C to return typeid. In many cases the actual returned type depends on the class implementation, many Apple supplied classes are class clusters and return a different type. A common example of this isNSStringwhich usually returns an instance of the classNSCFString.It is important to be careful about matching class names.
Also see the answer by Marc Charbonneau here