ios - 未找到 init 的实例方法(返回类型默认为 'id' )

标签 ios objective-c cocoa

我正在创建一个具有以下签名的 init 方法:

- (id)initWithDictionary:(NSDictionary *)dictionary AndLocation:(CLLocation *)location

但收到此警告消息:

Instance method '-initWithDictionary:AndLocation:' not found (return type defaults to 'id')

但是,如果我在 init 方法中放置一个断点,我肯定会这样:

  1. 为什么要发出警告?
  2. 我该如何摆脱它?

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/

相关文章:

ios - UITableView + UICollectionView + UITableView 大小问题

ios - 使用未解析的标识符 'present' - 当前方法不再有效

iOS:架构 x86_64 的 221 个重复符号 clang:错误:链接器命令失败,退出代码 1

objective-c - Objective C @autoreleasepool 指令

objective-c - 用于密集型科学应用的 NSDecimalNumber?

objective-c - NSInvocation 无参数

ios - 32 位 iOS 设备出现 CoreData 错误

ios - 在 Xcode 中,有没有办法搜索调用特定方法的所有行?

ios - 将 View 拖到界面生成器中的场景停靠栏中

objective-c - 本地插入符号位置 macos cocoa