I discovered an odd scenario that produces a compiler warning in XCode that I don't believe is a valid warning.
As an example I created two classes, ClassA & ClassB, that both have an init method called -initWithSomething: One takes an (NSDate *) as the "something" and the other takes (NSString *)
Class A
// ClassA.h
#import <Foundation/Foundation.h>
@interface ClassA : NSObject {
}
-(id)initWithSomething:(NSDate *)something;
@end
// ClassA.m
#import "ClassA.h"
@implementation ClassA
-(id)initWithSomething:(NSDate *)something {
if (self = [super init]) {
}
return self;
}
@end
Class B
// ClassB.h
#import <Foundation/Foundation.h>
@interface ClassB : NSObject {
}
-(id)initWithSomething:(NSString *)something;
@end
// ClassB.m
#import "ClassB.h"
@implementation ClassB
-(id)initWithSomething:(NSString *)something {
if (self = [super init]) {
}
return self;
}
@end
Implementation of another class that uses both ClassA & ClassB
#import "ExampleClass.h"
#import "ClassA.h"
#import "ClassB.h"
@implementation ExampleClass
-(void)doSomething {
NSDate *date = [NSDate date];
NSString *string = [NSString stringWithFormat:@"Test"];
ClassA *classA = [[ClassA alloc] initWithSomething:date];
ClassB *classB = [[ClassB alloc] initWithSomething:string]; // Produces "Incompatible pointer types sending 'NSString *' to parameter of type 'NSDate *'
ClassB *classB2 = [[ClassB alloc] initWithSomething:[NSString stringWithFormat:@"Test"]]; // Does NOT produce a warning
ClassB *classB3 = [[ClassB alloc] initWithSomething:@"Test"]; // Produces the same warning as above.
[classA release];
[classB release];
[classB2 release];
[classB3 release];
}
Is this a compiler bug? Doesn't seem like either of those lines should produce a warning, especially since the line where "classB2" is initted produces no warnings.
This code actually works fine, the correct class' -initWithSomething: is called and passed the appropriate type of argument.
Clearly, more explicit method names will avoid the issue but I'd like to know why the compiler isn't able to handle this.
Note: I should add that this only seems to occur with -init methods, any other instance or class functions do not seem to produce the warning.