Use xcode 5 to create cocoa touch static library (static library)

Source: Internet
Author: User

First, let's take a look at the knowledge about static databases:

Program compilation usually requires preprocessing, compilation, compilation, and linking. For some public code in our project, if you want to reuse it, You can compile the code into a static library file. In the link step, the linker obtains the corresponding code from the library file and generates an executable file. The execution file of the static library contains the complete code in the library, but multiple redundant copies will be generated when used multiple times.

The difference between a static library and a dynamic library is that the static library is copied at the link stage and has nothing to do with the running stage of the program. When the dynamic library is running, the system dynamically loads it into the memory for the program to call, this greatly saves memory.

In the previous use of xcode 5 to create its own framework, the blogger thought that some reusable code could be extracted from a framework. However, after some thinking, I personally think it is better to write a class library if you want to extract reusable classes into common interfaces for future programs. Although the framework is similar to the class library, all the components in the framework work together to solve a complicated problem, for example, the foundation framework aims to build the foundation of Cocoa/cocoa touch, and The Hibernate framework aims to achieve data persistence through Orm. Classes in the class library have their own functions and they have no common goals, so they are not correlated with each other.

The following describes how to use xcode 5 to create a static library and a project in the same workspace for testing.

1. Create a folder on the desktop and name it.

2. Open xcode and create a workspace file through the menu. Note that you should save it in the previous folder. As follows:


3. Right-click the project navigation area and create a project to the Workspace:


Select the following template:


4. Now you can add your class. For example:


Then, perform some challenging tasks and add a category, for example:

5. Open the build phases of target and add a copy headers phase to generate the public interface:


Add the following two header files to the newly added copy headers project and drag them to the public section above:


6. Select the iOS device and the corresponding IOS simulator, and build each time:


Right-click "show in Finder" in libjuliacore. A of products, and choose "show in Finder:


The iphoneos folder generates the library file that runs on the real machine device. The iphonesimulator folder generates the library file that runs in the simulator, libjuliacore. A is the built static library file. The usr/local/include directory stores the corresponding header files, that is, the header files published in public.

The static library is created successfully.

Next, test the interface provided in the static library file.

First, copy the libjuliacore. A header files in iPhone OS and iPhone simulator to a specific path.

1. Right-click a project in the workspace and create a project. Then, select a Common Program template, for example, single view application.

After creation, the workspace directory is as follows:


Add the previously copied libjuliacore to the project. A and. h file (both references and copies can be used). If it is run in the simulator, it copies the file in the simulator. If it is run on the real machine, it copies the file in the OS.

2. Next, call the interface shown in the header file, for example:

#import "ViewController.h"#import "JuliaCore.h"@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];        [JuliaCore showLibraryDescription];}@end

3. Run


4. It seems to have succeeded, but don't forget that there is still a category interface that hasn't been tested yet. Test code:

#import "ViewController.h"#import "JuliaCore.h"#import "NSString+Test.h"@implementation ViewController- (void)viewDidLoad{  [super viewDidLoad];  //[JuliaCore showLibraryDescription];    NSString *str = @"String";  NSLog(@"%@", [str stringWithJCPrefix:str]);}@end

Run... Crash.

2014-03-11 21:56:26.090 JuliaCoreDemo[3346:70b] -[__NSCFConstantString stringWithJCPrefix:]: unrecognized selector sent to instance 0x35b42014-03-11 21:56:26.095 JuliaCoreDemo[3346:70b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException‘, reason: ‘-[__NSCFConstantString stringWithJCPrefix:]: unrecognized selector sent to instance 0x35b4‘

Solution:

Select build settings in target of the running program, and set other link flags in linking to-all_load, as shown below:


Run:

2014-03-11 22:01:50.816 JuliaCoreDemo[3390:70b] JCString

5. To run a real machine, first copy the. A and. H files in the iPhone OS. Is there any way to simplify it? Yes! Merge the libjuliacore. A files in the iPhone OS and iPhone simulator, open the terminal, and enter the following command:

$ lipo -create ./os/libJuliaCore.a ./simulator/libJuliaCore.a -output ./JuliaCore-Merge.a

The first two paths are the paths of two. A files, and the subsequent paths are the generated. A file paths. You can customize the merged file names.


Note that the original two. A files in build settings must be consistent; otherwise, the merge fails.

The. A file after merge can be run on the real machine and simulator.

If a 64-bit iPhone simulator compilation error occurs, modify its ubuntures to armv7 and armv7s:


The biggest difference between such static library files and common open-source class libraries is that the static library does not publicly implement code and only provides header files. This will be useful for some scenarios where you do not want to disclose the source code. I used a static library lame that converts CAF to MP3:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.