我正在创建一个具有以下签名的 init
方法:
- (id)initWithDictionary:(NSDictionary *)dictionary AndLocation:(CLLocation *)location
但收到此警告消息:
Instance method '-initWithDictionary:AndLocation:' not found (return type defaults to 'id')
但是,如果我在 init
方法中放置一个断点,我肯定会这样:
- 为什么要发出警告?
- 我该如何摆脱它?
Fwiw,我已经重启了 xcode,删除了派生数据,Build Clean,等等。
这个方法签名不在 .h 文件中,但我很确定那里不应该存在 init
方法(例如 Objective-C: Should init methods be declared in .h?)。
最佳答案
您打算在实现文件之外使用的任何方法都必须在头文件中声明,否则您将收到警告(或错误)。这包括 init...
方法。
如果你真的不想在你的头文件中有一个 init...
方法(虽然我不确定你为什么不这样做),你可以创建一个静态构造方法而是镜像它。
@interface YourClass : YourSuperclass
+ (instancetype)createWithDictionary:(NSDictionary *)dictionary location:(CLLocation *)location;
@end
然后在你的实现文件中简单地实现它:
@implementation YourClass
+ (instancetype)createWitDictionary:(NSDictionary *)dictionary location:(CLLocation *)location {
return [[self alloc] initWithDictionary:dictionary location:location];
// or for non-ARC:
// return [[[self alloc] initWithDictionary:dictionary location:location] autorelease];
}
@end
关于ios - 未找到 init 的实例方法(返回类型默认为 'id' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728058/