Xcode中創(chuàng)建和添加Libary 實例操作
Xcode中創(chuàng)建和添加Libary 實例操作是本文要介紹的內(nèi)容,主要介紹如何在xcode中創(chuàng)建自定義libary,以及如何將外部Libary添加到項目中。
1.新建 "Libary" -> "Cocoa Touch static libary" 項目 "LibaryTest";
2.新建 "HellowWorld" 類:
- //HelloWorld.h
- #import <Foundation/Foundation.h>
- @interface HelloWorld : NSObject {}
- -(void)helloWorld;
- @end
- //---------------//
- HelloWorld.m
- #import "HelloWorld.h"
- @implementation HelloWorld
- -(void)helloWorld{ NSLog(@"hello world");
- }
- @end
3.新建 "Window-based Application" 項目 "TempTest";
4.在TempTest下新建“new Group” 名為 “Dependencies”;
5.在"Dependencies"下"Existing Files...",
選擇 "LibaryTest.xcodeproj",點擊"Add",
在彈出的面板中選中"Reference Type" 的 "Relative to project"選項, 然后確定.
6. 在 Dependencies 的詳情窗口勾選 LibaryTest.a 項,如圖(單擊可放大):
7.在LibaryTest.xcodeproj 上右鍵 "get info"窗口,我們可以看到它相對于當(dāng)前項目的相對路徑為"../LibaryTest/LibaryTest.xcodeproj".打開當(dāng)前項目的"get info"窗口,在build中搜索head,將會找到"Header Search Paths"項, 雙擊該選項,將"../LibaryTest"添加進去,如圖:
點OK. 這時在當(dāng)前項目(TempTest)中,應(yīng)該就能正常的提示LibaryTest中的類和方法了.
8.修改TempTestAppDelegate.m后,代碼如下:
- #import "TempTestAppDelegate.h"
- #import "HelloWorld.h"
- @implementation TempTestAppDelegate
- @synthesize window;
- #pragma mark -
- #pragma mark Application lifecycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch. HelloWorld *helloworld = [[HelloWorld alloc] init];
- [helloworld helloWorld];
- [self.window makeKeyAndVisible];
- return YES;}#pragma mark -#pragma mark Memory management- (void)dealloc {
- [window release];
- [super dealloc];}
- @end
"Command + B"編譯,沒有問題. 修改"LibaryTest"后編譯依然沒有問題,但" Command + R"運行可能會有問題.因為到目前為止,我們還只是將LibaryTest和當(dāng)前項目添加了關(guān)聯(lián),但在編譯當(dāng)前項目時,它不會去編譯LibaryTest庫. 所以我們還需要在當(dāng)前項目添加對LibaryTest的依賴關(guān)系.
9. 在打開Targets 下 TempTest 的 "get info"窗口. 選中g(shù)enaral標(biāo)簽. 在 "direct dependencies" 中 添加對LibaryTest的依賴.
小結(jié):Xcode中創(chuàng)建和添加Libary 實例操作的內(nèi)容介紹完了,希望本文對你有所幫助!