我们在做app或者做项目的时候经常用其它的类库 比如 uikit啦 corelocation 之类,只能看到里面的.h文件,.m文件是看不到的。怎么创建自己的类库呢,今天就来学习做一个吧。
首先就是打开xcode创建一个framework了
目录结构就是如上图所示了
然后去build settings 把类型改成静态的类库
紧接着 开始写 MyUtils.h 和 MyUtils.m文件
MyUtils.h
#import <Foundation/Foundation.h> @interface MyUtils : NSObject -(void)log:(NSString *)message; @end
MyUtils.m
#import "MyUtils.h" @implementation MyUtils -(void)log:(NSString *)message{ NSLog(@"MyUtils-log-%@",message); } @end
接下去 把 build settings 中的 MyUtils.h 从Project 移到 public
这个时候点金run 然后framework就生成了
点击上图中金黄色的文件夹 在右边你可以看到它的路径
直接点无效的话就用finder的 go to folder
现在你就可以看到自己的framework了
建个项目使用自己的framework把
#import "ViewController.h" #import <MyFirstFramework/MyFirstFramework.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. MyUtils *utils = [MyUtils new]; [utils log:@"HEllo"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
运行后 可以看到控制台打印出 Hello