指南创建您自己的库可可(触摸)发展 [英] A Guide for Creating your own Library for Cocoa(touch) development

查看:106
本文介绍了指南创建您自己的库可可(触摸)发展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用了很多自定义方法相同的子类的对象。这将是更方便的创建自己的库,我可以用几个项目

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.

我们的目标是有我自己的类被以同样的方式类,如的UIView 的CGRect 等可包括像 CGRectMake ,都与类和结构方便的方法。概括起来讲,我要创造我自己的等价物:

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


  • 类,如的UIView

  • 结构体如的CGRect

  • 便利的功能,如 CGRectMake

  • 有这可以作为一个库

  • 有这可以作为一个X code ++模板,因而具有这些自定义对象可作为X code'新文件

所以基本上我要找的指示,如何才能创造上述所有创建类,结构等。 什么是做到这一点吗 320项目似乎是一个好初始点。但它缺乏(我认为)在:

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 project seems like a good starting point. But it lacks (I think) in:


  • 有新项目中的库马上

  • 有下的新文件'提供的新类

即使我将创建一个自己的静态库的我将能够释放应用程序在App Store ,因为链接到第三方库不支持在手机上?

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?

为了您的方便,这些都是基本的子问题,涉及这一问题的范围:

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


  • 如何创建自己的库的Mac / iPhone发展?

  • 如何创建类,结构和内联函数为这个图书馆?

  • 如何创建基于此库我自己的X code ++模板?

  • 我能释放iPhone应用程序使用我自己的静态库?

  • 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上,你会建立一个框架。不过,你提到的UIView ,所以很明显你与iPhone合作。苹果不允许iPhone应用程序在运行时对其他库动态链接,所以你唯一的选择是创建一个静态库。静态库链接到时,它构建的应用程序的可执行文件。

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.

据我所知,有一个在X code无静态库项目模板。什么你可能需要做的就是开始与不同的iPhone X code ++模板,并添加静态库的目标。挂在默认应用程序目标;你可以用它来构建一个简单的测试应用程序,以确保库实际工作。

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.

要实际使用该库的应用程序,你需要两样东西:编译库(它有一个.A扩展)和所有的头文件。在你完成的应用程序,你会链接对你的静态库,所以编译器了解哪些类,函数等你需要#进口的头文件可给它。 (一种常用技术是创建一个导入所有其他人一个头文件。这样,你只需要输入一个文件在你的源文件。)

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 #import the 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.)

作为创建自己的自定义模板,这里有一个简单的教程,应该让你开始:<一href=\"http://www.macresearch.org/custom_x$c$c_templates\">http://www.macresearch.org/custom_x$c$c_templates你也许可以复制默认模板,只是对其进行自定义,以满足您的目的。

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

该结构的语法如下:

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.

创建结构的另一个小窍门就是做这样的事情:

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

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

由于编译器知道字段中的结构顺序,它可以很容易匹配您在大括号的相应字段提供值。当然,它更方便,有像 MyPointMake 的函数,因此你可以写这样的:

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:

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

请注意,这是一个功能,而不是一个方法,所以它生活的任何外部 @interface @implementation 背景。虽然我不认为编译器会抱怨,如果你在一个 @implementation 上下文定义它。

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

CGPointMake 的定义是什么被称为在线功能。你可以看到,在可可头文件的定义了。 (内联函数和正常功能的区别是编译器可以取代的呼叫 CGPointMake 复制 CGPointMake ,从而避免使一个函数调用的开销。这是一个pretty轻微的优化,但对于一个功能那么简单,它是有道理的。)

CGPointMake is defined as what's known as an inline function. 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 call to CGPointMake with a copy of 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.)

这篇关于指南创建您自己的库可可(触摸)发展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆