创建使用Objective-C的iOS框架

huangapple go评论66阅读模式
英文:

Creating iOS framework with Objective-C

问题

我在iOS上创建框架时遇到了一些问题。

我创建了一个新项目(名为Logger)并选择了“Framework”选项。
然后,Xcode创建了文件夹结构,其中包括Logger.h,但没有创建Logger.m

接下来,我创建了一个名为AppLogger的新Cocoa Touch文件,并添加了一些基本逻辑。

我在Logger.h中导入了AppLogger.h文件,像这样:

#import <Logger/AppLogger.h>

我也尝试过这样写:

#import "Logger/AppLogger.h"

但是它给我一个错误:

'Logger/AppLogger.h'文件未找到

我不明白我的设置有什么问题。
附上了我的项目结构的图像。

创建使用Objective-C的iOS框架

英文:

I have some trouble creating a framework for iOS.

I created a new project (called Logger) and selected the Framework option.
Then, Xcode created the folder structure and it had a few files including Logger.h, but no Logger.m was created.

Then, I have created new Cocoa Touch file called AppLogger and added some basic logic to it.

I imported the AppLoger.h file in my Logger.h like this:

#import &lt;Logger/AppLoger&gt;

I also tried it like this:

#import &quot;Logger/AppLoger&quot;

But it gives me an error:

&#39;Logger/AppLogger.h&#39; file not found

I don't understand what the problem is with my setup.
Attached is also an image of my project structure.

创建使用Objective-C的iOS框架

答案1

得分: 3

我刚刚在几天前经历了这个问题。您添加到主框架头文件的头文件应该是您框架的公共头文件。这意味着您需要将头文件标记为公共(而不是项目或私有)。

以下截图显示了您需要执行的操作:

创建使用Objective-C的iOS框架

  1. 选择应该是公共的头文件。
  2. 确保检查器面板显示在Xcode右侧。
  3. 选择“文件检查器”选项卡。
  4. 在“目标成员资格”下,确保头文件标记为“公共”。它默认为“项目”。

完成这些步骤后,您可以在Logger.h文件中添加以下行#import &lt;Logger/AppLogger.h&gt;


对于项目中的其他.h文件,您需要按照相同的步骤操作,但选择“项目”(而不是“公共”或“私有”)。并使用“正常”的导入方式。例如,在AppLogger.m中,您可能需要导入BaseLog.h。使用以下方式:

#import &quot;BaseLog.h&quot;

在将BaseLog.h设置为“项目”目标成员资格之后。

英文:

I just went through this myself a few days ago. The header files that you add to the main framework header are meant to be the public headers of your framework. This means you need to mark the header as being public (versus project or private).

The following screenshot shows what you need to do:

创建使用Objective-C的iOS框架

  1. Select the header that should be public.
  2. Ensure the Inspectors pane is being shown on the right side of Xcode.
  3. Select the File Inspector tab.
  4. Under Target Membership, ensure the header is marked as Public. It defaults to Project.

With those steps complete you can add the #import &lt;Logger/AppLogger.h&gt; line to the Logger.h file.


For other .h files in the project, you need to follow the same steps but select "Project" (not Public or Private). And use a "normal" import. For example, in AppLogger.m you might need to import BaseLog.h. Use:

#import &quot;BaseLog.h&quot;

after setting BaseLog.h was a target membership of "Project".

huangapple
  • 本文由 发表于 2023年7月20日 22:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76731017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定