Objective-C 找不到 .h 文件

Objective-C .h file not found

我昨天在 运行 我的项目中使用了这些导入文件:

#import "sip/NgnSipPreferences.h"
#import "services/impl/NgnBaseService.h"
#import "services/INgnSipService.h"

但今天我收到错误消息。它说找不到 'sip/NgnSipPreferences.h' 文件。但是当我删除 sip/ 使其成为 "NgnSipPreferences.h" 然后它被读取。

为什么会这样?我不想去掉头文件的来源路径,因为它太多了,我认为这是正确的方法。

我该如何解决这个问题?

#import "sip/NgnSipPreferences.h"

正在从文件夹 sip/ 导入 NgnSipPreferences.h,相对于导入它的文件。

所以你有 2 个解决方案,如果你不想更改 #import(通常最好按照你说的去做,将 imports/includes 留在适当的位置):

  1. 您将 .m 文件移动到适当的位置(在 sip/ 目录之上),或者
  2. 您将 sip/ 目录 上方 文件夹添加到 Build_Settings 中的 HEADER_SEARCH_PATHS 参数(例如,如果您在根文件夹,只需添加 ${SRCROOT} 而无需递归 - 在这种情况下,编译器肯定会找到您的头文件 ${SRCROOT}/sip/NgnSipPreferences.h).

Note: to get confidence with the topic, read Adding system header search path to Xcode

Note: if it was working up to yesterday, but not today, you can easily understand what happened if you installed GIT or another versioning tool, but sometimes Xcode is not cleaning all the caches properly, so your problem can be caused much before the date you thought. For this reason, not only "Clean" but also removing the directory DerivedData is quite useful! (I do regularly when something "strange" is happening...)