The static Library of iOS Development (ii) ——. A

Source: Internet
Author: User

The basic concepts and features of the static library in iOS are described earlier, and there is no more nonsense here, directly on the code

Compilation environment Xcode5.1 + MAC OS X 10.9.3

New Static Library project

There are two templates for creating a static library in Xcode, one for creating the iOS static library and the other for creating the Mac OS x static Library, which we chose

Click the Next button in the lower right corner to enter the next step

Give the project a name Mytoolsa, the rest by default

Click Next Next

All we care about is MyToolsA.h and Mytoolsam files.

  mytoolsa.h//  mytoolsa////  Created by LZH on 14-8-14.//  Copyright (c) 2014 LZH. All rights reserved.//#import <Foundation/Foundation.h> @interface mytoolsa:nsobject-(int) add: (int) x Second: ( int) y; Object method + (int) sub: (int) x Second: (int) y; Class Method @end
  mytoolsa.m//  mytoolsa////  Created by LZH on 14-8-14.//  Copyright (c) 2014 LZH. All rights reserved.//#import "MyToolsA.h" @implementation mytoolsa-(int) add: (int) x Second: (int) y{    return x + y;} + (int) sub: (int) x Second: (int) y{        return x-y;} @end

Start compiling after code completes

The project needs to be configured before compiling, because the debug version is generated by default and needs to be reconfigured if the release version needs to be generated

Xcode->product->scheme->edit Scheme ... Or simply press the shortcut key "Shift + Command + comma" to pop up the configuration panel

Change debug option to release option

Change the compilation target to iOS Device, indicating that the build is a static library of the real machine version

Click the triangle button in the upper left corner of the red box or the shortcut key "Command + B" to compile, when you see "Build succeeded" to indicate the success of the compilation

Then you can see that the LIBMYTOOLSA.A in the products directory has changed from red to black, indicating that we have successfully generated a static library file

Right-click libmytoolsa.a Select "Show in Finder"

Locate the folder where LIBMYTOOLSA.A

Switch the compilation target, change iOS Device to IPhone Retina (3.5-inch), compile and build a static library for the emulator

OK, now that the static library of the real-machine version and the emulator version have been generated, the following work is to merge the two, creating a static library that works both for the real machine and for the emulator

You need to use the following command

Lipo-create 1.a 2.a-output 12.a

Open terminal for convenient period, switch directly to super User

Locate the directory where the static library is located

Pop-up Properties menu, in the "location" attribute is the absolute path of the directory, copy and paste into the terminal window, you can enter the specified folder

Then use the Lipo command to synthesize the static library

Let's verify that the generated libraries support the platform we want.

As you can see, our resulting library LIBMYTOOLSA.A supports both 32-bit simulator architecture i386, 64-bit emulator architecture x86_64, 32-bit real-time architecture armv7/armv7s, and 64-bit true machine architecture arm64

is consistent with our expectations.

Here is the test project

Select a single view template for iOS projects here

Right-click on "New group" to create new "MyTools" and drag the library file libmytoolsa.a and header file MyToolsA.h just generated to MyTools

Attention:

Drag while holding down the ALT key, so that is copied instead of moving, the original files are still there, otherwise it will be the original location of the file deleted

Because. A static library must be used in conjunction with the header file, so also copy the. h file.

You must tick the staticlibatest target under Add to targets, otherwise you will not be able to use it even if you put the file in the build target.

To use the static library you just imported, just add the header file to

Modify the Viewcontroller.m file

  viewcontroller.m//  mytoolsatest////  Created by LZH on 14-8-15.//  Copyright (c) 2014 LZH. All rights reserved.//#import "ViewController.h" #import "MyToolsA.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload{    [Super viewdidload];//do any additional setup after loading the view, typically FR Om a nib.        int x = ten, y = 7;        Use the class method    NSLog (@ "%d-%d =%d", x, Y, [Mytoolsa sub:x second:y]);        Using the object method    Mytoolsa *mytoolsa = [[Mytoolsa alloc] init];        NSLog (@ "%d +%d =%d", x, Y, [Mytoolsa add:x second:y]);        } -(void) didreceivememorywarning{    [Super didreceivememorywarning];    Dispose of any resources the can be recreated.} @end

Operation Result:

Analog device

Real Machine

OK, finished, the output is exactly the same as we expected.

The static Library of iOS Development (ii) ——. A

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.