xcode 为 Cocoa(touch) 开发创建自己的库的指南

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:30:55  来源:igfitidea点击:

A Guide for Creating your own Library for Cocoa(touch) development

cxcodeoopframeworks

提问by Kriem

I'm currently using a lot of the same subclassed objects with custom methods. It would be more convenient to create my own library which I can use for several projects.

我目前正在使用许多具有自定义方法的相同子类对象。创建可以用于多个项目的自己的库会更方便。

The goal is to have my own classes being available in the same way classes like UIView, CGRectetc are, including convenient methods like CGRectMake, both with classes and structs. To sum it up, I want to create my own equivalents of:

我们的目标是有我自己的类是用同样的方法类,如可用UIViewCGRect等等都是,包括方便的方法一样CGRectMake,都与类和结构。总而言之,我想创建自己的等价物:

  • Classes like UIView
  • Structs like CGRect
  • Convenient functions like CGRectMake
  • Have this available as a library
  • Have this available as an XCode template, thus, having these custom Objects available as 'new files' in XCode
  • 类如 UIView
  • 结构如 CGRect
  • 方便的功能如 CGRectMake
  • 将此作为图书馆提供
  • 将此作为 XCode 模板提供,因此,将这些自定义对象作为 XCode 中的“新文件”提供

So basically I'm looking for instructions on how to create classes, structs etc in order to create all the above. What is the best way to do this?The 320 projectseems like a good starting point. But it lacks (I think) in:

所以基本上我正在寻找有关如何创建类、结构等的说明,以便创建上述所有内容。做这个的最好方式是什么?320项目似乎是一个很好的起点。但它缺乏(我认为):

  • having the library available in new projects right away
  • having the new classes available under 'new file'
  • 立即在新项目中使用该库
  • 在“新文件”下提供新类

Even if I would create an own static library, will I be able to release the app on the app store, since linking to 3rd party libraries is not supported on the phone?

即使我会创建自己的静态库,我能否在应用商店上发布该应用,因为手机不支持链接到 3rd 方库?

For your convenience, these are basically the sub questions, covering the scope of this question:

为了您的方便,这些基本上是子问题,涵盖了这个问题的范围:

  • How can I create my own library for Mac / iPhone development?
  • How do I create classes, structs and inline function for this library?
  • How do I create my own Xcode template based on this library?
  • Will I be able to release iPhone apps using my own static library?
  • 如何为 Mac/iPhone 开发创建自己的库?
  • 如何为此库创建类、结构和内联函数?
  • 如何基于这个库创建我自己的 Xcode 模板?
  • 我可以使用我自己的静态库发布 iPhone 应用程序吗?

采纳答案by Alex

If you were doing this for a Mac, you'd create a framework. However, you mention UIView, so obviously you're working with the iPhone. Apple doesn't allow iPhone applications to dynamically link against other libraries at runtime, so your only option is to create a static library. A static library is linked into the application executable when it's built.

如果您为 Mac 执行此操作,您将创建一个框架。但是,您提到UIView,很明显您正在使用 iPhone。Apple 不允许 iPhone 应用程序在运行时动态链接其他库,因此您唯一的选择是创建一个静态库。静态库在构建时链接到应用程序可执行文件中。

To my knowledge, there's no static library project template in Xcode. What you'll likely have to do is start with a different iPhone Xcode template and add a Static Library target. Hang on to the default application target; you can use that to build a simple test application to make sure the library actually works.

据我所知,Xcode 中没有静态库项目模板。您可能需要做的是从不同的 iPhone Xcode 模板开始并添加一个静态库目标。坚持默认的应用程序目标;您可以使用它来构建一个简单的测试应用程序,以确保该库确实有效。

To actually use the library in an application, you'll need two things: the compiled library (it has a .a extension) and all the header files. In your finished application, you'll link against your static library, and you'll need to #importthe header files so that the compiler understands what classes, functions, etc. are available to it. (A common technique is to create one header file that imports all the others. That way, you only need to import a single file in your source files.)

要在应用程序中实际使用该库,您需要两样东西:已编译的库(它具有 .a 扩展名)和所有头文件。在您完成的应用程序中,您将链接到您的静态库,您将需要#import头文件,以便编译器了解哪些类、函数等可用。(一种常见的技术是创建一个导入所有其他头文件的头文件。这样,您只需在源文件中导入一个文件。)

As for creating your own custom templates, there's a simple tutorial here that should get you started: http://www.macresearch.org/custom_xcode_templatesYou can probably copy the default templates and just customize them to suit your purposes.

至于创建您自己的自定义模板,这里有一个简单的教程可以帮助您入门:http://www.macresearch.org/custom_xcode_templates您可能可以复制默认模板并根据您的目的自定义它们。

The struct syntax looks like this:

结构体语法如下所示:

typedef struct _MyPoint {
    CGFloat x;
    CGFloat y;
} MyPoint;

Structs are are declared in header files, so you can (I believe) Command+Double Click on the name of a struct to see how it's declared.

结构在头文件中声明,因此您可以(我相信)Command+Double 单击结构名称以查看它是如何声明的。

Another little trick for creating structs is to do something like this:

创建结构的另一个小技巧是做这样的事情:

MyPoint aPoint = (MyPoint){ 1.5f, 0.25f };

Because the compiler knows the order of fields in the struct, it can very easily match up the values you provide in the curly braces to the appropriate fields. Of course, it's more convenient to have a function like MyPointMake, so you can write that like this:

因为编译器知道结构体中字段的顺序,所以它可以很容易地将您在花括号中提供的值与适当的字段相匹配。当然,有一个像 的函数更方便MyPointMake,所以你可以这样写:

MyPoint MyPointMake(CGFloat x, CGFloat y) 
    return (MyPoint){ x, y };
}

Note that this is a function, not a method, so it lives outside of any @interfaceor @implementationcontext. Although I don't think the compiler complains if you define it in an @implementationcontext.

请注意,这是一个函数,而不是一个方法,因此它存在于 any@interface@implementation上下文之外。虽然我不认为编译器会抱怨如果你在@implementation上下文中定义它。

CGPointMakeis defined as what's known as an inlinefunction. You can see the definition for that in the Cocoa header files, too. (The difference between an inline function and a normal function is that the compiler can replace a callto CGPointMakewith a copyof CGPointMake, which avoids the overhead of making a function call. It's a pretty minor optimization, but for a function that simple, it makes sense.)

CGPointMake被定义为所谓的inline函数。您也可以在 Cocoa 头文件中看到它的定义。(内联函数和正常功能之间的区别是编译器可以取代呼叫CGPointMake一个复制CGPointMake,这避免了进行一个函数调用的开销。这是一个非常小的优化,但是对于这么简单,它使一个功能感觉。)

回答by lottadot

FYI Xcode 3.2 has a new project template called Cocoa Touch Static Library. You might want to go that route.

仅供参考 Xcode 3.2 有一个名为 Cocoa Touch Static Library 的新项目模板。你可能想走那条路。

回答by drewh

The 320 projectis a good example of an iPhone class library. You basically compile your project down into a .a library and then statically link against this in your client projects.

320项目是一个iPhone类库的一个好例子。您基本上将您的项目编译成一个 .a 库,然后在您的客户端项目中静态链接到这个库。

回答by David Salzer

Since this is a community wiki now, I thought it will be helpful to link some resources and tutorials:

由于这是一个社区维基,我认为链接一些资源和教程会有所帮助:

http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html

http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html

http://www.clintharris.net/2009/iphone-app-shared-libraries/

http://www.clintharris.net/2009/iphone-app-shared-libraries/

Enjoy!

享受!

回答by Peter Hosey

The 320 project seems like a good starting point indeed. But it lacks (I think) in:

  • having the library available in new projects right away
  • having the new classes available under 'new file'

320 项目似乎确实是一个很好的起点。但它缺乏(我认为):

  • 立即在新项目中使用该库
  • 在“新文件”下提供新类

Those are project and file templates. For more information, ask the Google.

这些是项目和文件模板。如需更多信息,请咨询 Google

回答by Brian

If you plan on releasing this on the app store, you wont be able to use your library in the way that you would like. As mentioned above, linking to 3rd party libraries is not supported on the phone. I think there is a 'hack' way to make it work, but you'll lose distribution.

如果您打算在应用程序商店上发布它,您将无法以您喜欢的方式使用您的库。如上所述,手机不支持链接到第 3 方库。我认为有一种“hack”方式可以让它工作,但你会失去分发。

The best I could come up with was putting all the relevant code in a directory and sharing it that way. I know its not as elegant, but its their limitation ie. out of our control.

我能想到的最好方法是将所有相关代码放在一个目录中并以这种方式共享。我知道它不那么优雅,但它的局限性即。超出我们的控制。