iOS 組件化開發(fā)實踐

一、 對組件的理解

組件是由一個或多個類構(gòu)成,能完整描述一個業(yè)務(wù)場景,并能被其他業(yè)務(wù)場景復(fù)用的功能單位。組件就像是PC時代個人組裝電腦時購買的一個個部件,比如內(nèi)存,硬盤,CPU,顯示器等,拿出其中任何一個部件都能被其他的PC所使用。

所以組件可以是個廣義上的概念,并不一定是頁面跳轉(zhuǎn),還可以是其他不具備UI屬性的服務(wù)提供者,比如日志服務(wù),內(nèi)存管理服務(wù)等等。說白了我們目標(biāo)是站在更高的維度去封裝功能單元。對這些功能單元進(jìn)行進(jìn)一步的分類,才能在具體的業(yè)務(wù)場景下做更合理的設(shè)計。

組件化開發(fā),就是將一個臃腫,復(fù)雜的單一工程的項目, 根據(jù)功能或者屬性進(jìn)行分解,拆分成為各個獨立的功能模塊或者組件 ; 然后根據(jù)項目和業(yè)務(wù)的需求,按照某種方式, 任意組織成一個擁有完整業(yè)務(wù)邏輯的工程。

它的優(yōu)點

  • 項目結(jié)構(gòu)清晰
  • 代碼邏輯清晰
  • 快速集成
  • 代碼復(fù)用率高
  • 迭代效率高

如果你正在面試,或者正準(zhǔn)備跳槽,不妨看看我精心總結(jié)的面試資料:https://gitee.com/Mcci7/i-oser 來獲取一份詳細(xì)的大廠面試資料 為你的跳槽加薪多一份保障

二、 創(chuàng)建私有庫流程

1. 創(chuàng)建私有組件 -- 相當(dāng)于是從github上下載組件模板

pod lib create XYImageDetail

在創(chuàng)建的過程中需要配置一些參數(shù)

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )

What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]
 > No

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > Yes
 Putting demo application back in, you cannot do view tests without a host application.

What is your class prefix?
 > XY

根據(jù)提示即會生成pod工程模板(如下圖所示)

image

代碼文件放在目錄"/XYImageDetail/XYImageDetail/Classes/"中

2.編輯podspec文件

兩種打開方式:
第一種是cd到工程根目錄vim XYImageDetail.podspec;
第二種是Xcode 打開Example,在第個文件夾Podspec Metadata中可以直接編輯

Pod::Spec.new do |s|
  s.name             = 'XYImageDetail'
  s.version          = '0.1.0'
  s.summary          = '簡短描述:圖片瀏覽器'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here. 這里寫一大串 組件描述信息--巴拉巴拉寫一堆
                       DESC

  s.homepage         = 'https://github.com/周述堅/XYImageDetail'   #  這個鏈接是需要可以訪問的鏈接
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '周述堅' => '394002732@qq.com' }
  s.source           = { :git => 'https://github.com/周述堅/XYImageDetail.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'
  #  下面這是資源路徑
  s.source_files = 'XYImageDetail/Classes/**/*'

  # s.resource_bundles = {
  #   'XYImageDetail' => ['XYImageDetail/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  #  下面是添加依賴庫
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

3. 創(chuàng)建遠(yuǎn)程代碼倉庫

由于在創(chuàng)建私有庫的時候,就已經(jīng)創(chuàng)建了.git文件,創(chuàng)建完遠(yuǎn)程代碼倉庫后,與本地創(chuàng)建的私有庫相關(guān)聯(lián)。

git remote add origin http://github.com/zhoushujian/xyimagedetail.git
git add .
git commit -m "Initial commit"
git push origin master

此時代碼就已經(jīng)傳到遠(yuǎn)程代碼倉庫。

4. 給私有庫打tag

git tag '0.1.0'
git push --tags

這里需要注意的是tag的版本號需要與XYImageDetail.podspec中的s.version = '0.1.0'保持一致。

5. 創(chuàng)建遠(yuǎn)程索引庫

在git上創(chuàng)建一個遠(yuǎn)程倉庫來作為pod的索引庫,來存放.podspec文件,可以是私有的也可以是公開的
創(chuàng)建完之后 會有一個遠(yuǎn)程倉庫地址例如:

https://github.com/zhoushujian/XYSpecs.git

查看本地索引庫

pod repo

添加自己的本地索引庫,并關(guān)聯(lián)創(chuàng)建好的遠(yuǎn)程git倉庫

pod repo add XYImagedetail https://github.com/zhoushujian/XYSpecs.git

在push 之前首先驗證一下本地的.podspec文件,如果驗證不通過,是上傳不了的。

pod lib lint --allow-warnings  // 本地驗證
pod spec lint  --allow-warnings // 遠(yuǎn)程驗證

把本地.podspec文件push到遠(yuǎn)程索引庫

pod repo push XYImageDetail XYImageDetail.podspec // 如果有警告無法通過,可以在后面加--allow-warnings 

至此,私有庫創(chuàng)建完成

注意:在后期對組件進(jìn)行迭代更新的時候,注意要給私有庫打tag。否則在pod的時候,依然是舊版本。另外,如果在本機(jī)pod install時,提示上傳的組件未找到,可以嘗試pod repo update 命令。再進(jìn)行pod install。

作者:zhoushujian
鏈接:http://www.itdecent.cn/p/e520d1cca248

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容