我發現了一個奇怪的場景,它在XCode中產生了一個編譯器警告,我不相信這是一個有效的警告。不匹配-init方法名
作爲一個例子我創建兩個類,ClassA的& ClassB的,在都具有稱爲-initWithSomething一個init方法: 一個接受一個(的NSDate *)作爲 「東西」,而另一個爲(的NSString *)
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
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
同時使用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];
}
另一個類的實現這是一個編譯器錯誤?似乎這些行中的任何一行都不應該產生警告,尤其是因爲「classB2」所在的行不會產生警告。
此代碼實際上工作正常,調用了正確的類'-initWithSomething:並傳遞了適當類型的參數。
顯然,更明確的方法名稱將避免這個問題,但我想知道爲什麼編譯器無法處理這個問題。
注意: 我要補充,這似乎只與-init的方法來進行,其他任何實例或類的功能似乎並沒有產生警告。
我不知道爲什麼有人會低估這個,這是一個很好的問題 – 2010-10-31 13:41:55