The establishment and use of the IOS Static class library project

Source: Internet
Author: User

New Xcode Workspace

Open Xcode, choose File---New, Workspace, name Workspace test.xcworkspace, and select the appropriate directory.

New Static Library Project

Select File, New, Project, select the Cocoa Touch Static Library, project name Mylib.xcodeproj, and select Use Automatic referen Ce counting.

Xcode will automatically generate the MyLib.h and MYLIB.M files in your project, click the MyLib.h file, and add the following two method definitions:

12 - (NSInteger) add:(NSInteger)a and:(NSInteger)b;+ (NSString*) connect:(NSString*)str1 and:(NSString*)str2;

Then open the MYLIB.M file and add the implementation that just defined the two files:

-(Nsinteger) Add: (Nsinteger) A and: (Nsinteger) b {   return a + b;} + (nsstring*) Connect: (NSString *) str1 and: (NSString *) str2 {   return [nsstring stringwithformat:@ "%@%@", STR1, str2] ;}

Now, the final file looks like this:

1234567891011121314151617181920212223242526272829303132333435363738 //// MyLib.h// MyLib//// Created by gdeic on 4/16/12.// Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import <foundation foundation.h="">@interface MyLib : NSObject- (NSInteger) add:(NSInteger)a and:(NSInteger)b;+ (NSString*) connect:(NSString*)str1 and:(NSString*)str2;@end//// MyLib.m// MyLib//// Created by gdeic on 4/16/12.// Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import "MyLib.h"@implementation MyLib - (NSInteger) add:(NSInteger)a and:(NSInteger)b {   returna + b;}+ (NSString*) connect:(NSString *)str1 and:(NSString *)str2 {   return[NSString stringWithFormat:@"%@ %@", str1, str2];}@end</foundation>

Select the Mylib project, select the Target of the project in the Middle Editor window, select the Build Phases tab, expand the Copy Headers group, and below are three sub-groups, public, Project and Private, MyLib.h drag and drop to the public group.

Save all files, select Product--build, compile, build the Libmylib.a file, and copy the MyLib.h file to the output directory.

Working with static class library projects

Select File, New, Project, item template Select Single View application, application, IOS, project name named MYAPP, note the use St Oryboards and use Automatic Reference counting.

After the project is built, the project window looks like this:

Drag the Mylib project to the frameworks folder of the MYAPP project, select Create groups for any added folders in the Pop-up dialog box, then click the Finish button.

With the MYAPP project selected, under Select the target of the project, check the Summary tab to find the Linked frameworks and Library grouping options, such as:

Click the plus button below to add the LIBMYLIB.A of the workspace.

Next add the header file search directory, select Targets above Project, select the Build Settings tab, enter header search in the search box to filter, find the header search Paths, add a row, enter .. /mylib, and select the Recursive check box.

Now to verify that the reference to Mylib is correct, open the VIEWCONTROLLER.M for the MYAPP project, add a reference to the MyLib.h, as shown, and compile MYAPP, if the compilation succeeds, it means that the reference is correct.

Open the Mainstoryboard.storyboard file, add two Uitextfield, two UIButton, and a UILabel on the generated viewcontroller as shown in:

and add the corresponding outlet and action, ViewController.h as follows:

1234567891011121314151617181920212223 //// ViewController.h// MyApp//// Created by gdeic on 4/19/12.// Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import <uikit uikit.h="">@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *textField1;@property (weak, nonatomic) IBOutlet UITextField *textField2;@property (weak, nonatomic) IBOutlet UILabel *resultLabel;- (IBAction)addButtonClick:(id)sender;- (IBAction)connectButtonClick:(id)sender; @end</uikit>

Open the viewcontroller.m file, implement Addbuttonclick: And Connectbuttonclick: Method, call Mylib instance method in Addbuttonclick: Method Add:and:, in Conn Ectbuttonclick: The static method of calling Mylib in the method Connect:and: as follows:

123456789101112131415161718192021 - (IBAction)addButtonClick:(id)sender {   // 获取用户输入的两个数字   NSInteger num1 = [self.textField1.text integerValue];   NSInteger num2 = [self.textField2.text integerValue];   // 初始化一个新的 MyLib 实例   MyLib* myLib = [[MyLib alloc] init];   // 调用实例方法相加   NSInteger result = [myLib add:num1 and:num2];   // 显示结果   self.resultLabel.text = [NSString stringWithFormat:@"%d + %d = %d", num1, num2,result];}- (IBAction)connectButtonClick:(id)sender {   // 获取用户输入的两个字符串   NSString* str1 = self.textField1.text;   NSString* str2 = self.textField2.text;   // 调用 MyLib 的静态方法连两个字符串   NSString* result = [MyLib connect:str1 and:str2];   // 显示结果   self.resultLabel.text = result;}

When you click the Add button, the effect looks like this:

When you click the Connect button, the effect looks like this:

The establishment and use of the IOS Static class library project

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.