创建static library项目
代码方法
// MathOperation.h
#import <Foundation/Foundation.h>
@interface MathOperation : NSObject
- (NSInteger)addNumber:(NSInteger)number1 toNumber:(NSInteger)number2;
@end
// MathOperation.m
#import "MathOperation.h"
@implementation MathOperation
- (NSInteger)addNumber:(NSInteger)number1 toNumber:(NSInteger)number2 {
NSInteger sum = number1 + number2;
return sum;
}
@end
配置
- 这个设备类型和打包配置要和等下使用这个文件的项目工程打包配置一一对应
编译并导出
- 等下把上面两个文件放到新的项目工程文件中
新建一个项目工程
- 拉取上面生成的两个文件
- 然后在targets->build phases -> link binary with libraries 引入include的头文件
引入并使用
//
// ViewController.m
// Math
//
// Created by dengzhipeng on 2023/12/21.
//
#import "ViewController.h"
#import "MathOperation.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
MathOperation *mathOperation = [[MathOperation alloc] init];
NSInteger result = [mathOperation addNumber:3 toNumber:5];
NSLog(@"The sum is: %ld", (long)result);
}
@end
有多个编译配置生成的a合并
- 使用终端执行命令
# ~ 表示文件路径 自己电脑路径为主
lipo -create ~/Debug-iphoneos/MathOperation.a ~/Debug-iphonesimulator/MathOperation.a -output ~/MathOperation.a