让你快速上手组件化

为什么要组件化?

  • 随着项目的不断迭代,各个模块会越来越复杂,各个模块相互依赖,而且每个模块可能会有共同的业务逻辑,导致整个项目维护起来比较麻烦
  • 可以采用组件化,把每个业务逻辑和模块分离,单独管理,这样比较方便维护,各个开发人员只需要关心好自己的模块就好了。

一.如何组件化?

以下的SunHomeKit即为组件代称

  • 1.在托管平台创建私有索引库

  • 2.将私有索引库上传到cocoapods
    pod repo add 名称 地址

  • 3.创建组件项目

cd 目录
pod lib create BuHomeKit

4.把代码放在classes文件夹,拖入工程
注:下面的SunHomeKit的编译选项要勾选

  • 5.修改SunHomeKit.podspec文件
//版本
s.version = '0.1.0'
//描述
s.description = 'home'
//仓库地址
s.source = { :git => 'https://git.coding.net/SayHi_/SunSpecKit.git', :tag => s.version.to_s }
//哪一个目录下(**是所有文件)(从当前spec文件的目录下开始)
s.source_files = 'SunHomeKit/Classes/**/*'
  • 6.编译
  • 7.上传到托管平台
cd XMGHomeKit
git status
git add .    //提交缓存
git commit - m ‘0.1.0’    //提交到本地
git remote   //查看远程有无
git remote add origin https://git.coding.net/SayHi_/BuToolSpectKit.git
git push origin master
  • 8.上传索引库到cocoapods
8.1.打标签
git tag -a 0.1.0 -m   ‘0.1.0’
git push —tags
8.2.上传
pod repo push 索引库名称 SunHomeKit.podspec —allow-warnings

二.组件的使用

  • 1.查看地址
    pod repo
  • 2.修改podfile:
source ‘https://github.com/CocoaPods/Specs.git' 
source ‘https://git.coding.net/SayHi_/SunSpecKit.git’
pod 'SunHomeKit', '~> 0.1.0'
  • 3.执行
cd 目录
pod install

三.子组件的划分

1.修改podspec:

//Frame 与 frame不能同名
//Frame为文件夹的名称,frame是临时变量(记得检查路径对错)
//此行代码需要被注释
#  s.source_files = 'SunHomeKit/Classes/**/*'

  s.subspec 'Frame' do |frame|
    frame.source_files = 'SunHomeKit/Classes/Frame/*.{h,m}'
  end

  s.subspec 'Color' do |color|
    color.source_files = 'SunHomeKit/Classes/Color/*.{h,m}'
  end

三.组件中添加依赖库

  • 1.SunHomeKit.podspec中添加:
s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'BuDeJieToolKit'
  • 2.指行Podfile文件
    PodFile文件中 pod 'SunHomeKit', :path => ‘../‘ 的意思是找到上级目录中的xxx.podspec
cd 目录
pod install

四.图片资源组件化

  • 1.图片加入到 组件名称/Assets/ 文件夹中
  • 2.修改podspec文件:
s.resource_bundles = {
     'SunHomeKit' => ['SunHomeKit/Classes/**/*.xib','SunHomeKit/Assets/Home/**/*']
  }
  • 3.指行Podfile文件
cd 目录 
pod install
  • 4.编译
  • 5.在Product文件中找到SunHomeKit.bundle
    打开包内容查看图片资源是否正确添加
  • 6.修改组件的图片名称:
    原名称为xx,现在改为 组件名称.bundle/xx
  • 7.上传到托管平台

五.oc代码集成swift组件

注:swift中使用oc使用桥接,oc使用swift是不使用桥接的

  • 1.修改spec:
    s.dependency 'swift库名称'
  • 2.执行Podfile文件
cd 目录
pod install 
  • 3.查找文件
pods-第三方库targets-buildsetting中搜索swift compiler - general
查看头文件的正确名称 
  • 4.头文件导入
库名/正确名称
一般为:库名/库名 -Swift.h

六:组件化常见错误

  • 1.storyboard报错:

1.1.修改spec:

s.source_files = 'SunHomeKit/Classes/**/*.{h,m}'
s.resource_bundles = {
    'XMGLiveKit' => ['SunHomeKit/Classes/**/*.{storyboard,xib}']
  }

1.2.修改代码:

原代码

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@“Live” bundle:nil];

修改后:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];    
NSString *sName = [NSString stringWithFormat:@"%@.bundle/Live",bundle.infoDictionary[@"CFBundleName"]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:sName bundle:bundle];
  • 2.错误:
    x86_64:
    “_inflate”, referenced from:
    _http_read_stream in IJKMediaFramework(http.o)
缺少依赖的框架,需要引入
(如果不对再查Stack Overflow)
  • 3.找不到cell的错误:

原代码:

[self.tableView registerNib:[UINib nibWithNIbName:@“XMGLiveCell” bundle:nil] forCellReuseIdentifier:ID];

修改后:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *nibName = [NSString stringWithFormat:@"%@.bundle/XMGLiveCell",bundle.infoDictionary[@"CFBundleName"]];
[self.tableView registerNib:[UINib nibWithNibName:nibName bundle:bundle] forCellReuseIdentifier:ID];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,188评论 4 61
  • 枕畔只影 独睡难眠 忆往昔 钦差红花喧天贺 佳人红妆入梦来 高马入仕傲 娇妻怀中笑 一朝醒 我自以为 终 虚无缥缈...
    cy走啊阅读 157评论 2 4
  • 以前特害怕右眼皮跳,一跳特准,要么出点小交通事故,要么破点小财,要么家里有点小矛盾。可2015年迄今为止两次右眼皮...
    Molly海龟阅读 258评论 0 0
  • JavaScript 中作用域有两种主要的工作模式,第一种是词法作用域,另外一种叫做动态作用域。 大部分标准语言编...
    白色鹈鹕鸟阅读 165评论 0 0