让你快速上手组件化

2017-09-19  本文已影响6人  攻克乃还_

为什么要组件化?

一.如何组件化?

以下的SunHomeKit即为组件代称

cd 目录
pod lib create BuHomeKit

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

//版本
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/**/*'
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.1.打标签
git tag -a 0.1.0 -m   ‘0.1.0’
git push —tags
8.2.上传
pod repo push 索引库名称 SunHomeKit.podspec —allow-warnings

二.组件的使用

source ‘https://github.com/CocoaPods/Specs.git' 
source ‘https://git.coding.net/SayHi_/SunSpecKit.git’
pod 'SunHomeKit', '~> 0.1.0'
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

三.组件中添加依赖库

s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'BuDeJieToolKit'
cd 目录
pod install

四.图片资源组件化

s.resource_bundles = {
     'SunHomeKit' => ['SunHomeKit/Classes/**/*.xib','SunHomeKit/Assets/Home/**/*']
  }
cd 目录 
pod install

五.oc代码集成swift组件

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

cd 目录
pod install 
pods-第三方库targets-buildsetting中搜索swift compiler - general
查看头文件的正确名称 
库名/正确名称
一般为:库名/库名 -Swift.h

六:组件化常见错误

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];
缺少依赖的框架,需要引入
(如果不对再查Stack Overflow)

原代码:

[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];
上一篇下一篇

猜你喜欢

热点阅读