I found a strange script that causes a compiler warning in Xcode, which I believe is not a valid warning.
As an example, I created two classes: ClassA and ClassB, which have an init method called -initWithSomething: One accepts (NSDate *) as "something" and the other accepts (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
An implementation of another class that uses both ClassA and 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];
Is this a compiler error? It seems that none of these lines should cause warnings, especially since the line in which "classB2" is included does not cause any warnings.
This code really works fine, the correct class is' -initWithSomething: is called and passed the corresponding type of argument.
Obviously, more explicit method names will avoid this problem, but I would like to know why the compiler cannot handle this.
Note : I have to add that this is only like the -init methods, any other functions of the instance or class do not seem to generate warnings.
source share