对于每一位开发者而言,开发经验积累到了一定的程度后,都想对自己的东西进行封装,方便更快捷的使用。 这里跟大家分享一下,静态库SDK的制作过程。如果有不对的地方,希望大家指正。
制作的库,可分为静态库跟动态库。静态库在加载时系统会将全部代码都复制一份到项目中去,换然之,这会造成一定程度的代码冗余。动态库则刚好相反,只要系统中有了一份,就算不同的应用也会公用一份。但是也就说明了使用动态库的应用不能上线到苹果商店的。
前提:你开发好了某一模块的功能,并且已经将改功能的代码分离出来。
这里以页面滑动模块作为例子。
1.新建这样的一个工程:

2.一般来说,制作的库都想要做到最大程度的兼容,这里设置一下

3.将开发好的模块代码加载进来

注意:如果你的模块功能的代码用到类别(category)的,在category类中的所有属性都必须使用objc_setAssociatedObject进行扩展,否则制作好的静态库在使用是会报错(ps:类目只扩展方法,不可扩展属性)。

4.制作模拟器跟真机上的.a文件
模拟器:



command+B 编译,成功后,可以看到

同样,真机的.a文件生成。
真机:


command+B 编译,成功后,可以看到

查看:


5.将两个.a文件合并,方便使用。打开终端。
输入:lipo -create 模拟器.a文件的路径 真机.a文件的路径 -output 合并后的.a文件路径


6.使用制作的.a库


只用运行后,编译没问题,在加载的时候会报错:没法识别某个方法.
需要设置:

重新运行。ok


demo链接:https://github.com/xjl1411817045/XJL_Slide_Project.git
网友评论