[组件化]06.业务组件抽取上传中遇到的问题总结

上传过程不在赘述了,这篇文章主要记录一下业务组件抽取过程中遇到的问题,
组件化的完整Demo在此可以下载:这里下载 (请切到feature_business分支,该分支是组件化完成后的Demo)。
依赖问题
1. XiB和storyboard的依赖问题
XiB和storyboard被抽成组件放到Pods文件夹中后,就不在mainBundle中了,如图显示products:

显示包内容,发现XiB和storyboard确实不在此bundle目录中:

而是在其Frameworks目录下,如下图所示位置:

所以按照以前加载mainBundle中的XiB和storyboard的话,已经加载不出来了,解决办法如下图所示:

根据上图中po出来的bundle路径,前往文件中确实找到了storyboard。
XiB也是一样,按以前加载XiB的方式,运行会出现如下错误所示:


上图中的方式是默认从mainBundle中加载XiB,改成以下方式,从当前类所在bundle中加载XiB就好了:
let bundle = Bundle(for: SFDiscoverViewController.self)
tableView.register(UINib(nibName: String(describing: SFDiscoverServicesCell.self), bundle: bundle), forCellReuseIdentifier: String(describing: SFDiscoverServicesCell.self))
2. 图片资源依赖问题
图片资源也是一样,抽到组件中后就不在mainBundle中了,不能从mainBundle中加载出来了,也存在图片资源依赖问题,解决此问题步骤如下:
2.1 将图片资源拖到本地模版库与Classes同级的文件夹Assets中:

同时打开spec文件中的资源加载路径:

然后cd 到example工程后pod install安装,就会看到多了个Resource文件夹。

2.2 改变加载方式
项目中通过[UIImage imageNamed:@“图片名"]
加载图片的方式,是默认从mainBundle中去加载图片的,需要改成以下方式:
func imagePath(imageName: String, imageFormat: String) -> UIImage{
let bundle = Bundle(for: SFDiscoverCycleScrollCell.self)
let fullImageName = "\(imageName)@2x.\(imageFormat)"
guard let path = bundle.path(forResource: fullImageName, ofType: nil, inDirectory: "SFCloudMusicDiscoverKit.bundle"), let image = UIImage(contentsOfFile: path) else {
return UIImage()
}
return image
}
要注意的是:mainBundle会识别图片的尺寸和格式,而自定义bundle不会,所以这里图片的全名要包含尺寸和后缀,而且path也要指定图片所在bundle包名。寻找图片所在bundle的方式跟XiB或storyboard一样,如下红圈部分就是图片所在bundle:

经过以上操作就能解决图片显示问题了。
3.对其他组件的依赖
如果某个业务组件对其他组件有依赖,在验证本地模版库时会报以下错误:
pod lib lint --verbose --allow-warnings

这是因为验证podspec文件时默认只会到官方索引库 (https://github.com/CocoaPods/Specs.git)中去校验,而我们的业务组件中依赖了的其他组件,需要同时指定自己创建的远程索引库地址库中校验。解决办法如下:
pod lib lint --verbose --allow-warnings --sources='https://github.com/shcamaker/SFCloudMusicBaseKitSpec.git,https://github.com/CocoaPods/Specs.git'
pod spec lint 验证也是一样。
上传完成后,pod search时可能会遇到以下错误:

此时可以尝试删除~/Library/Caches/CocoaPods目录下的search_index.json文件,再次pod setup后,再执行pod search即可。

另外在安装库时,遇到以下问题:

解决办法:
修改podfile文件中依赖的platform的版本,将其设置为更高一点的版本:比如
platform :ios, '9.0'
如果不行,继续增加:
platform :ios, '10.0'