I’ve been coding up some Cocoa for my own ‘enjoyment’ and I’ve come across something that had me rather scratching my head for a while. In my project I’ve currently got 4 classes and in one of these classes I have made an initialiser called
-(Graph*)initWithCapactity:(int)size;
in this method I’m then using a set of NSMutableArrays to hold a list of variables an trying to be a good obj-c citizen I’m initialising them by:
NSMutableArray *y = [[NSMutableArray alloc] initWithCapacity:graphSize];
Then when I compile it using the Clang compiler I get the warning:
Graph.m:45:23 Graph.m:45:23:{45:23-45:74}: warning: multiple methods named 'initWithCapacity:' found
It turns out this is caused by the compiler not quite understanding that I’m using the initWithCapacity method of the NSMutableArray and not anything else, the simple fix to this is to cast the NSMutableArray to a NSMutableArray – this seems pretty retarded to me to be honest, casting an object to its type when I’m creating it…
Anyway the “correct” code is:
NSMutableArray *y = [(NSMutableArray*)[NSMutableArray alloc] initWithCapacity:graphSize];
Not exactly the elegance that I’ve come to like about obj-c…