1

经过大量的 C# 经验后,我只是在学习 Objective-c。我非常想念的一件事是能够在一个单独的项目中编写扩展方法,我可以在我的所有项目中引用它。这是一些天真的c#:

public static bool IsShortString(this string s) {
    return s.length <= 3;
}

在 Visual Studio 中,我可以只添加一个引用 an using,而 bammyString.IsShortString()将是一个相当无用的方法。

所以我想我想写一个静态库,但我不确定我从那里去哪里。

还有一个问题,如果我确实编写了这个静态库,我是否能够使用一个#import指令在库中的各种文件中使用所有方法,还是必须单独导入每个标头?

4

2 回答 2

1

The closest thing in objective-c is categories.

This is also a good tutorial on categories.

于 2012-04-13T14:42:50.280 回答
1

What you are looking for is called Category, and it allows you to add some additional methods to existing classes. Check the reference http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

You can create your own toolkit which is a static library containing categories you made. Common practice is to create one header file containing imports for all the headers in the lib, so when using it, you just do

#import "libName.h"

Also, when creating a static library containing categories it is important to include -all_load and -ObjC linker flags to your project.

于 2012-04-13T14:46:57.940 回答