转自:http://www.devdiv.com/thread-61384-1-1.html
Why Use Static Libraries
There are a number of reasons to use static libraries, but the main two are distribution to others without revealing source code, and code reuse across projects.
1. Distribution:
Say you have a great idea for a view that given a few images, creates a beautiful photo mosaic. Now, others might want to use your awesome mosaic class as part of their fart-light (fart machine flashlight) app to make mosaic fart-lights from your photo library.
You have done a ton of work to make your mosaic app and don’t want to just give away the code. $$$ you decide to sell your code. However, would-be thieves might not honor your awesome license and begin to redistribute the code as if it were their own (oh noooz).
Boom static library.
Bundle up your code into a static library and package it with the header files. Now others can purchase your library, use it in their own apps to make fart mosaics without revealing the source and you can continue to sell copy after copy.
2. Code reuse:
So, you have like 30 apps in the store that do just about nothing. However they all have some common code, say a farting engine. Rather than dragging all of the source files in and having redundant code all over the place, you can just compile your fart engine
once and use the library in all of your projects.
Where Can I Get Me One Of These?
Well, let’s dig in and create a sample static library. The project we are going to create is code-only (remember we are not going over resources in this tutorial) and will be a simple math library with two methods exposed. It will be able to compute fibonacci
numbers given number n and compute a factorial value given number n (I know, pretty dang exciting).
Step 1: Starting a New Static Library Project
This is pretty straight forward as Apple has made it pretty simple to create a static library.
Open XCode and start a new project. Under iOS, select Library and “Cocoa Touch Static Library”. We are going to name ours ICodeMathUtils. This will create a nice new project for us that builds a .a file.
One thing to note is, NO class files get created by default. Don’t freak out, we will create some manually.
Step 2: Code your static library
First we need to add some files. Add a new NSObject subclass to your project and name it MathFunctions.m.
Add this code to the header file:
-
#import
-
-
@interface MathFunctions : NSObject {
-
}
-
-
- (NSArray *) fibonacci:(NSInteger) n;
-
- (NSInteger) factorial:(NSInteger) n;
-
-
@end
复制代码
Nothing fancy, just our method declarations. When we distribute the library file, this header file MUST be included as well.
One thing I should point out here is don’t make every iVar public! As a newer developer (or a stubborn lazy one: Im looking at you @cruffenach), you might think it’s cool to make a public property out of EVERYTHING. This is bad form and here is where it will
bite you. You don’t want the external world being able to muck with things that you don’t want them to.
And Now the .m file for the implementation:
-
#import "MathFunctions.h"
-
-
@implementation MathFunctions
-
-
- (NSArray *) fibonacci:(NSInteger) n {
-
-
NSMutableArray *fib = [NSMutableArray array];
-
-
int a = 0;
-
int b = 1;
-
int sum;
-
int i;
-
-
for (i=0;i < n;i++)
-
{
-
[fib addObject:[NSNumber numberWithInt:a]];
-
sum = a + b;
-
a = b;
-
b = sum;
-
}
-
-
return (NSArray *) fib;
-
}
-
-
- (NSInteger) factorial:(NSInteger) n {
-
if ( n <= 1 )
-
return 1;
-
else
-
return n * [self factorial:( n-1 )];
-
}
-
@end
复制代码
This code should be very familiar to any CS 101 student : ). And that’s it for our static library. The best way to test this puppy out before distributing is to create a new unit test target, set up some unit tests, and check it out that way. That tutorial
is for another day.
Building And Distributing Your Library
Once you are happy with your library, simply build it in XCode. Obviously, don’t do build and run as you can’t run it (again unless you have a test suite). Now, look under the Products group in XCode and you should see a file called lib(libraryName).a. In our
case, it’s libICodeMathUtils.a.
Right click on that file and select “Reveal In Finder”. Drag this file into a new folder that you create where you will be bundling all of your library files. I just created a folder on my desktop named iCodeBlogsMathLibrary. Now, do the same with all of the
.h files. In our case, just copy MathFunctions.h into this new directory. Your directory structure should now look like:
iCodeBlogsMathLibarby
|- libICodeMathUtils.a
|- MathFunctions.h
Now you can zip this folder and sell it to would-be iOS developers for millions!
Linking Your Library In A New Project
So now that you have built your shiny new static library, it’s time to test it out in another application. XCode has a number of ways to actually achieve this, but I will show the most simple and Mac-like… drag and drop : ).
Create a new View-Based project (or whatever it doesn’t really matter). I named mine MathTest.
Now, just drag this folder into the project and XCode will set up all of the linking automagically. When prompted to copy, I usually say yes, but you can do whatever you want depending on how you intend on using the library. Sometimes just linking and not copying
is far more beneficial if you have multiple projects sharing a single library. It ensures that they all use the most up to date version.
You should now see the .a file along with the header files in the new project.
Using The Static Library Code
Now that all of the linking is set up, you just use your library like any other class. In the App Delegate class of my test project, I simple did this:
-
// Import at the top
-
#import "MathFunctions.h"
复制代码
And in the applicationDidFinishLaunchingMethod…
-
MathFunctions *mFunctions = [[[MathFunctions alloc] init] autorelease];
-
NSLog(@"fibonacci for 10 = %@", [mFunctions fibonacci:10]);
-
NSLog(@"10! = %d",[mFunctions factorial:10]);
复制代码
This of course prints out:
-
2011-04-07 11:48:49.528 MathTest[854:607] fibonacci for 10 = (
-
0,
-
1,
-
1,
-
2,
-
3,
-
5,
-
8,
-
13,
-
21,
-
34
-
)
-
2011-04-07 11:48:49.533 MathTest[854:607] 10! = 3628800
复制代码
And there it is! Static library code being executed in a totally separate project.
Conclusion
Like I said earlier, static libraries can be quite helpful in your iPhone development endeavors. I feel that they are quite underused, but could really save time and money in the long run.
分享到:
相关推荐
iOS静态库项目的创建和使用源码
自己亲手写了一个制作framework静态库的demo,简单易懂,把自己的经常使用到的代码封装成静态库,使用方便还可以防止别人偷窃自己的代码。很实用。
iOS 静态库创建成功后的测试
在ios开发中使用静态库,静态库中包括用obc写的两个函数和c写的一个函数。
主要为大家详细介绍了iOS创建与使用静态库的相关资料,感兴趣的小伙伴们可以参考一下
希望对大家有所帮助!介绍还是满详细的,从零开始的
创建一个最简单的IOS静态库工程,和调用方法。包含2个工程。
IOS创建静态Framework模板,执行后会在xcode生成静态framework模板 因为动态framework的生成需要IOS8.0以上的系统才能支持,否则不能上架,所以在支持IOS7.0或者以下的代码的时候需要使用静态framework 1.确保...
创建静态库是在项目中重用代码的好方法,但是使用Xcode 4,如果您不知道要设置它的以下步骤,则可能很难设置。 而且,如果您的静态库定义了动态的类别,则需要采取额外的步骤来确保您的实现链接所有内容,以便在运行...
ObjCrust一种经过修改的ObjCrust,它使用Rust交叉编译器。 交叉编译器需要首先构建(注意:现在在单独的分支上,所以不要使用ObsCrust进行修改。使用ObjCrust的ObstCrust使用Rust交叉编译器。交叉编译器需要首先进行...
前言 开发中经常使用三方库去实现某特定功能,而这些三方库通常又分为开源库和闭源库。开源库可以直接拿到源码,和自己写...静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝。 动态库:链接时不
静态表一个用于快速创建程序化表格视图以显示首选项甚至更多动态内容的库。... iOS7之前的iOS不支持动态框架,而Swift不支持静态库。 在“构建阶段”选项卡中时,在“目标依赖项”下添加libStaticTabl
xamarin.ios-opencv 这是在Xamarin.iOS上使用OpenCV示例项目 ...Xamarin解决方案:绑定项目:为静态库创建一个C#包装程序,iOS应用程序:演示静态库的绑定和功能,检测图像上的面Kong并在图像上用红色矩形绘制它们
该项目构建了一个提供 sip 连接功能的 iOS 静态库。 它公开了 DXIPJSipManager 类,该类可用于将 iOS 应用程序连接到 sip 服务器。 1 - 添加到项目 #####a) 添加 pspip-ios 作为子模块 打开终端并导航到您的项目...
ios-comscore
攻略21 在滚动视图中使用静态内容 79 攻略22 创建旋转翻页的滚动视图 82 第3章 绘图攻略 86 攻略23 绘制梯度填充的贝塞尔轨迹 87 攻略24 创建多个动画的动态图像 91 攻略25 创建组合与变换的视图 93 ...
pod trunk push创建静态库使用通过以下生成的静态库。 为创建一个发行iphoneos 发布模拟器调试iphoneos 调试模拟器通过例如加入调试库和发布库libtool -static -o libUniversalRelease.a Release-iphoneos/...
适用于iOS,macOS(以及和 )的Lottie 在查看文档,常见问题解答,帮助,示例等 Lottie是适用于Android和iOS的移动库,它以最少的代码实时本地渲染...您可以拉出存储库,并包含Lottie.xcodeproj来构建动态或静态库。