我在Objective-C中遇到一个奇怪的问题,当我有两个类使用相同名称的初始化器,但是类型不同的参数.例如,假设我创建了类A和B:
啊:
#import <Cocoa/Cocoa.h>
@interface A : NSObject {
}
- (id)initWithNum:(float)theNum;
@end
Run Code Online (Sandbox Code Playgroud)
上午:
#import "A.h"
@implementation A
- (id)initWithNum:(float)theNum
{
self = [super init];
if (self != nil) {
NSLog(@"A: %f", theNum);
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
BH:
#import <Cocoa/Cocoa.h>
@interface B : NSObject {
}
- (id)initWithNum:(int)theNum;
@end
Run Code Online (Sandbox Code Playgroud)
BM:
#import "B.h"
@implementation B
- (id)initWithNum:(int)theNum
{
self = [super init];
if (self != nil) {
NSLog(@"B: %d", theNum);
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
main.m文件:
#import <Foundation/Foundation.h>
#import "A.h" …
Run Code Online (Sandbox Code Playgroud)