首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

学习如何封装Framework

2024-12-08 来源:要发发知识网

一. Framework的制作

首先新建一个Framework文件

新建一个HelloWorld文件,继承NSObject。

#import "HelloWorld.h"

@implementation HelloWorld

- (void)helloWorld {
    NSLog(@"Hello World!");
}

@end

更改这些参数

加入以下参数,将Build Active Architecture Only 设置为NO。

在Build Phase里面的Header加入自己新建的HelloWorld.h

在主文件引入HelloWorld.h

#import <UIKit/UIKit.h>

//! Project version number for NinePhotoFramework.
FOUNDATION_EXPORT double NinePhotoFrameworkVersionNumber;

//! Project version string for NinePhotoFramework.
FOUNDATION_EXPORT const unsigned char NinePhotoFrameworkVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <NinePhotoFramework/PublicHeader.h>

#import <NinePhotoFramework/HelloWorld.h>

在真机、模拟器环境上编译文件,有以下报错

invalid iOS deployment version '-mios-simulator-version-min=12.1', iOS 10 is the maximum deployment target for 32-bit targets

在General把Deployment Target设成10.0

再次编译,得到Products

image.png

右键进入Show in Finder,退到Products文件夹,看到有两个生成的文件

然后用命令行把这两项合并。

...have the same architectures (armv7) and can't be in the same fat output file

还是回去把armv7删了吧。。好像删了就行了

二. 使用Framework

将Framework导入另一个project,并在APPDelegate操作:

#import <NinePhotoFramework/NinePhotoFramework.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    HelloWorld *hello = [[HelloWorld alloc] init];
    [hello helloWorld];
}

大功告成!

显示全文