为 Cocoa(touch) 开发创建自己的库的指南 [英] A Guide for Creating your own Library for Cocoa(touch) development

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

问题描述

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

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.

目标是让我自己的类以与 UIViewCGRect 等类相同的方式可用,包括方便的方法,如 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
  • 等方便的函数
  • 将此作为库提供
  • 将其作为 XCode 模板提供,从而在 XCode 中将这些自定义对象作为新文件"提供

所以基本上我正在寻找有关如何创建类、结构等的说明,以便创建上述所有内容.最好的方法是什么? 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:

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

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

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 开发创建自己的库?
  • 如何为这个库创建类、结构和内联函数?
  • 如何基于这个库创建自己的 Xcode 模板?
  • 我能否使用自己的静态库发布 iPhone 应用?

推荐答案

如果你是为 Mac 做这个,你会创建一个框架.但是,您提到了 UIView,因此显然您正在使用 iPhone.Apple 不允许 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.

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

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)和所有头文件.在您完成的应用程序中,您将链接到您的静态库,并且您需要 #import 头文件,以便编译器了解哪些类、函数等对其可用.(一种常见的技术是创建一个头文件来导入所有其他头文件.这样,您只需在源文件中导入一个文件.)

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.)

至于创建您自己的自定义模板,这里有一个简单的教程可以帮助您入门:http://www.macresearch.org/custom_xcode_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;

结构是在头文件中声明的,所以你可以(我相信)Command+双击结构的名称来查看它是如何声明的.

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 被定义为所谓的 inline 函数.你也可以在 Cocoa 头文件中看到它的定义.(内联函数和普通函数的区别在于编译器可以将 call 替换为 CGPointMakecopyCGPointMake,它避免了进行函数调用的开销.这是一个非常小的优化,但对于这么简单的函数来说,它是有意义的.)

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.)

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

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