文章目錄
- 開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)
- 1. SwiftHub項目簡介
- 2. SwiftHub項目編譯,用到的第三方庫簡介
- 2.1 SwiftHub項目編譯
- 2.2 SwiftHub項目用到的第三方框架
- 2.2.1 網絡庫
- 2.2.2 數據解析庫
- 2.2.2.1 [ObjectMapper](https://github.com/Hearst-DD/ObjectMapper)
- 2.2.2.2 [Moya-ObjectMapper/Swift](https://github.com/ivanbruel/Moya-ObjectMapper)
- 2.2.3 Rxswift 框架和相關擴展
- 2.2.3.1 [RxDataSources](https://github.com/RxSwiftCommunity/RxDataSources)
- 2.2.3.2 [RxSwiftExt](https://github.com/RxSwiftCommunity/RxSwiftExt)
- 2.2.3.3 [NSObject+Rx](https://github.com/RxSwiftCommunity/NSObject-Rx)
- 2.2.3.4 [RxViewController](https://github.com/devxoul/RxViewController)
- 2.2.3.5 [RxGesture](https://github.com/RxSwiftCommunity/RxGesture)
- 2.2.3.6 [RxOptional](https://github.com/RxSwiftCommunity/RxOptional)
- 2.2.3.7 [RxTheme](https://github.com/RxSwiftCommunity/RxTheme)
- 2.2.3.8 [RxAnimated](https://github.com/RxSwiftCommunity/RxAnimated)
- 2.2.4 圖像處理庫
- 2.2.5 資源文件管理庫
- 2.2.5.1 [R.swift ](https://github.com/mac-cain13/R.swift)
- 2.2.5.2 [SwiftLint](https://github.com/realm/SwiftLint)
- 2.2.6 祕鑰管理庫
- 2.2.7 自動佈局庫
- 2.2.8 UI相關庫
- 2.2.8.1 [NVActivityIndicatorView](https://github.com/ninjaprox/NVActivityIndicatorView)
- 2.2.8.2 [ImageSlidershow/Kingfisher](https://github.com/zvonicek/ImageSlideshow)
- 2.2.8.3 [DZNEmptyDataSet](https://github.com/dzenbot/DZNEmptyDataSet)
- 2.2.8.4 [Hero](https://github.com/lkzhao/Hero)
- 2.2.8.5 [Localize-Swift](https://github.com/marmelroy/Localize-Swift)
- 2.2.8.6 [RAMAnimatedTabBarController](https://github.com/Ramotion/animated-tab-bar)
- 2.2.8.7 [AcknowList](https://github.com/vtourraine/AcknowList)
- 2.2.8.8 [KafkaRefresh](https://github.com/OpenFeyn/KafkaRefresh)
- 2.2.8.9 [WhatsNewKit](https://github.com/SvenTiigi/WhatsNewKit)
- 2.2.8.10 [Highlightr](https://github.com/raspu/Highlightr)
- 2.2.8.11 [DropDown](https://github.com/AssistoLab/DropDown)
- 2.2.8.12 [Toast-Swift](https://github.com/scalessec/Toast-Swift)
- 2.2.8.13 [HMSegmentedControl](https://github.com/HeshamMegid/HMSegmentedControl)
- 2.2.8.14 [FloatingPanel](https://github.com/SCENEE/FloatingPanel)
- 2.2.8.15 [MessageKit](https://github.com/MessageKit/MessageKit)
- 2.2.8.16 [MultiProgressView](https://github.com/mac-gallagher/MultiProgressView)
- 2.2.8.17 [IQKeyboardManagerSwift](https://github.com/hackiftekhar/IQKeyboardManager)
- 2.2.9 日誌管理庫
- 2.2.10 數據埋點庫
- 2.2.10.1 [Umbrella](https://github.com/devxoul/Umbrella)
- 2.2.10.2 [Umbrella/Mixpanel]()
- 2.2.10.3 [Umbrella/Firebase]()
- 2.2.10.4 [Mixpanel](https://github.com/mixpanel/mixpanel-iphone)
- 2.2.10.5 [Firebace/Analytics]()
- 2.2.11 廣告工具點庫
- 2.2.12 性能優化相關庫
- 2.2.13 其他工具類庫
- 2.2.13.1 [FLEX](https://github.com/Flipboard/FLEX)
- 2.2.13.2 [SwifterSwift](https://github.com/SwifterSwift/SwifterSwift)
- 2.2.13.3 [BonMot](https://github.com/Rightpoint/BonMot)
- 2.2.13.4 [DateToolsSwift](https://github.com/MatthewYork/DateTools)
- 2.2.13.5 [SwiftDate](https://github.com/malcommac/SwiftDate)
- 3. SwiftHub項目採用的架構分析
開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)
1. SwiftHub項目簡介
SwiftHub 是大神Khoren Markosyan 寫的一個完全採用Rxswift + MVVM + Moya 的架構的項目,代碼很精簡,想學習MVVM架構的認真去研究這個項目的設計,對你以後的編程思想和習慣都會有很大的幫助。(點擊這裏下載:SwiftHub源碼 )
2. SwiftHub項目編譯,用到的第三方庫簡介
2.1 SwiftHub項目編譯
下載源碼後,進入SwiftHub-master主目錄,先要下載安裝第三方庫,如果你cd SwiftHub-master/
就直接執行pod install
的話一般都會報錯:
分析報錯原因不難看出,已經提示我們需要先pod repo update
一下更新你本地的cocos pod庫。
可能有的小夥伴網速不太好,pod install一直更新不了,這裏提供了一份我編譯好的源碼:鏈接:https://pan.baidu.com/s/1qwkjY_ZrgV9Y5yudiyVJdQ 密碼:60t7
2.2 SwiftHub項目用到的第三方框架
-
我只能驚歎,哇塞,怎麼用了這麼多第三方框架啊,我個人觀點是不太主張用太多第三方框架,能自己實現都自己實現,除非要實現的功能必須要用第三方框架。因爲第三方框架會大大增加我們ipa包的大小,對於ipa大小有要求的是個災難,例如之前我們有一個項目使用
Realm
作爲DB框架,但是發現這個框架實在是太佔內存了足足有將近90MB,而我只是想裏面一個小小的數據庫存儲相關的代碼,後面改成WCDB.swift
框架,這個框架只有2MB左右。 -
下面我們先來看一下SwiftHub 項目用到的第三方框架吧:
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
use_frameworks!
inhibit_all_warnings!
target 'SwiftHub' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
# Pods for SwiftHub
# Networking
pod 'Moya/RxSwift', '14.0.0-beta.2' # https://github.com/Moya/Moya
pod 'Apollo', '0.19.0' # https://github.com/apollographql/apollo-ios
# Rx Extensions
pod 'RxDataSources', '~> 4.0' # https://github.com/RxSwiftCommunity/RxDataSources
pod 'RxSwiftExt', '~> 5.0' # https://github.com/RxSwiftCommunity/RxSwiftExt
pod 'NSObject+Rx', '~> 5.0' # https://github.com/RxSwiftCommunity/NSObject-Rx
pod 'RxViewController', '~> 1.0' # https://github.com/devxoul/RxViewController
pod 'RxGesture', '~> 3.0' # https://github.com/RxSwiftCommunity/RxGesture
pod 'RxOptional', '~> 4.0' # https://github.com/RxSwiftCommunity/RxOptional
pod 'RxTheme', '~> 4.0' # https://github.com/RxSwiftCommunity/RxTheme
#pod 'RxAnimated', '~> 0.4' # https://github.com/RxSwiftCommunity/RxAnimated
# JSON Mapping
#pod 'ObjectMapper', :git => 'https://github.com/kajensen/ObjectMapper.git' # https://github.com/Hearst-DD/ObjectMapper
pod 'Moya-ObjectMapper/RxSwift', :git => 'https://github.com/khoren93/Moya-ObjectMapper.git', :branch => 'moya14' # https://github.com/ivanbruel/Moya-ObjectMapper
# Image
pod 'Kingfisher', '~> 5.0' # https://github.com/onevcat/Kingfisher
# Date
pod 'DateToolsSwift', '~> 4.0' # https://github.com/MatthewYork/DateTools
pod 'SwiftDate', '~> 6.0' # https://github.com/malcommac/SwiftDate
# Tools
pod 'R.swift', '~> 5.0' # https://github.com/mac-cain13/R.swift
pod 'SwiftLint', '0.37.0' # https://github.com/realm/SwiftLint
# Keychain
pod 'KeychainAccess', '~> 4.0' # https://github.com/kishikawakatsumi/KeychainAccess
# Fabric
pod 'Fabric'
pod 'Crashlytics'
# UI
pod 'NVActivityIndicatorView', '~> 4.0' # https://github.com/ninjaprox/NVActivityIndicatorView
pod 'ImageSlideshow/Kingfisher', '~> 1.8' # https://github.com/zvonicek/ImageSlideshow
pod 'DZNEmptyDataSet', '~> 1.0' # https://github.com/dzenbot/DZNEmptyDataSet
pod 'Hero', '~> 1.5.0' # https://github.com/lkzhao/Hero
pod 'Localize-Swift', '~> 3.0' # https://github.com/marmelroy/Localize-Swift
pod 'RAMAnimatedTabBarController', '~> 5.0' # https://github.com/Ramotion/animated-tab-bar
pod 'AcknowList', '~> 1.8' # https://github.com/vtourraine/AcknowList
pod 'KafkaRefresh', '~> 1.0' # https://github.com/OpenFeyn/KafkaRefresh
pod 'WhatsNewKit', '~> 1.0' # https://github.com/SvenTiigi/WhatsNewKit
pod 'Highlightr', '~> 2.0' # https://github.com/raspu/Highlightr
pod 'DropDown', '~> 2.0' # https://github.com/AssistoLab/DropDown
pod 'Toast-Swift', '~> 5.0' # https://github.com/scalessec/Toast-Swift
pod 'HMSegmentedControl', '~> 1.0' # https://github.com/HeshamMegid/HMSegmentedControl
pod 'FloatingPanel', '~> 1.0' # https://github.com/SCENEE/FloatingPanel
pod 'MessageKit', '~> 3.0' # https://github.com/MessageKit/MessageKit
pod 'MultiProgressView', '~> 1.0' # https://github.com/mac-gallagher/MultiProgressView
# Keyboard
pod 'IQKeyboardManagerSwift', '~> 6.0' # https://github.com/hackiftekhar/IQKeyboardManager
# Auto Layout
pod 'SnapKit', '~> 5.0' # https://github.com/SnapKit/SnapKit
# Code Quality
pod 'FLEX', :git => 'https://github.com/khoren93/FLEX.git', :branch => 'remove_private_api' # https://github.com/Flipboard/FLEX
pod 'SwifterSwift', '~> 5.0' # https://github.com/SwifterSwift/SwifterSwift
pod 'BonMot', '~> 5.0' # https://github.com/Rightpoint/BonMot
# Logging
pod 'CocoaLumberjack/Swift', '~> 3.0' # https://github.com/CocoaLumberjack/CocoaLumberjack
# Analytics
# https://github.com/devxoul/Umbrella
pod 'Umbrella/Mixpanel', '~> 0.8'
pod 'Umbrella/Firebase'
pod 'Mixpanel', '~> 3.0' # https://github.com/mixpanel/mixpanel-iphone
pod 'Firebase/Analytics'
# Ads
pod 'Firebase/AdMob'
pod 'Google-Mobile-Ads-SDK', '7.52.0'
target 'SwiftHubTests' do
inherit! :search_paths
# Pods for testing
pod 'Quick', '~> 2.0' # https://github.com/Quick/Quick
pod 'Nimble', '~> 8.0' # https://github.com/Quick/Nimble
#pod 'RxNimble', '~> 4.0' # https://github.com/RxSwiftCommunity/RxNimble
pod 'RxAtomic', :modular_headers => true
pod 'RxBlocking' # https://github.com/ReactiveX/RxSwift
pod 'Firebase'
end
end
target 'SwiftHubUITests' do
inherit! :search_paths
# Pods for testing
end
post_install do |installer|
# Cocoapods optimization, always clean project after pod updating
Dir.glob(installer.sandbox.target_support_files_root + "Pods-*/*.sh").each do |script|
flag_name = File.basename(script, ".sh") + "-Installation-Flag"
folder = "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
file = File.join(folder, flag_name)
content = File.read(script)
content.gsub!(/set -e/, "set -e\nKG_FILE=\"#{file}\"\nif [ -f \"$KG_FILE\" ]; then exit 0; fi\nmkdir -p \"#{folder}\"\ntouch \"$KG_FILE\"")
File.write(script, content)
end
# Enable tracing resources
installer.pods_project.targets.each do |target|
if target.name == 'RxSwift'
target.build_configurations.each do |config|
if config.name == 'Debug'
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['-D', 'TRACE_RESOURCES']
end
end
end
end
end
- 接下來,我們來分析這些第三方庫都是用來幹什麼,說不定哪天你的項目也可以用到呢。
2.2.1 網絡庫
2.2.1.1 Alamfire
2.2.1.2 Rxswift
2.2.1.3 Moya
- 源碼下載:Moya
2.2.2 數據解析庫
2.2.2.1 ObjectMapper
- 源碼下載: ObjectMapper
2.2.2.2 Moya-ObjectMapper/Swift
2.2.3 Rxswift 框架和相關擴展
2.2.3.1 RxDataSources
- 源碼下載:RxDataSources
2.2.3.2 RxSwiftExt
- 源碼下載:RxSwiftExt
2.2.3.3 NSObject+Rx
- 源碼下載:NSObject+Rx
2.2.3.4 RxViewController
- 源碼下載:RxViewController
2.2.3.5 RxGesture
- 源碼下載:RxGesture
2.2.3.6 RxOptional
- 源碼下載: RxOptional
2.2.3.7 RxTheme
- 源碼下載: RxTheme
2.2.3.8 RxAnimated
- 源碼下載: RxAnimated
2.2.4 圖像處理庫
2.2.4.1 Kingfisher
2.2.5 資源文件管理庫
2.2.5.1 R.swift
- 源碼下載:R.swift
2.2.5.2 SwiftLint
- 源碼下載: SwiftLint
2.2.6 祕鑰管理庫
2.2.6.1 KeychainAccess
- 源碼下載: KeychainAccess
2.2.7 自動佈局庫
2.2.7.1 SnapKit
- 源碼下載: SnapKit
2.2.8 UI相關庫
2.2.8.1 NVActivityIndicatorView
2.2.8.2 ImageSlidershow/Kingfisher
2.2.8.3 DZNEmptyDataSet
- 源碼下載: DZNEmptyDataSet
2.2.8.4 Hero
- 源碼下載:Hero
2.2.8.5 Localize-Swift
- 源碼下載: Localize-Swift
2.2.8.6 RAMAnimatedTabBarController
2.2.8.7 AcknowList
- 源碼下載: AcknowList
2.2.8.8 KafkaRefresh
- 源碼下載:KafkaRefresh
2.2.8.9 WhatsNewKit
- 源碼下載: WhatsNewKit
2.2.8.10 Highlightr
- 源碼下載: Highlightr
2.2.8.11 DropDown
- 源碼下載:DropDown
2.2.8.12 Toast-Swift
- 源碼下載:Toast-Swift
2.2.8.13 HMSegmentedControl
- 源碼下載: HMSegmentedControl
2.2.8.14 FloatingPanel
- 源碼下載: FloatingPanel
2.2.8.15 MessageKit
- 源碼下載: MessageKit
2.2.8.16 MultiProgressView
- 源碼下載: MultiProgressView
2.2.8.17 IQKeyboardManagerSwift
2.2.9 日誌管理庫
2.2.9.1 CocoaLumberjack/Swift
2.2.10 數據埋點庫
2.2.10.1 Umbrella
- 源碼下載: Umbrella
2.2.10.2 Umbrella/Mixpanel
2.2.10.3 Umbrella/Firebase
2.2.10.4 Mixpanel
- 源碼下載:Mixpanel
2.2.10.5 Firebace/Analytics
2.2.11 廣告工具點庫
2.2.11.1 Firebase/AdMob
2.2.11.2 Google-Mobile-Ads-SDK
2.2.12 性能優化相關庫
2.2.12.1 Fabric
2.2.12.2 Crashlytics
2.2.13 其他工具類庫
2.2.13.1 FLEX
- 源碼下載: FLEX
2.2.13.2 SwifterSwift
- 源碼下載: SwifterSwift
2.2.13.3 BonMot
- 源碼下載:BonMot
2.2.13.4 DateToolsSwift
- 源碼下載:DateToolsSwift
2.2.13.5 SwiftDate
- 源碼下載: SwiftDate