欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Swift

當(dāng)前位置:主頁 > 軟件編程 > Swift >

如何快速用上Swift靜態(tài)庫詳解

來源:本站原創(chuàng)|時間:2020-01-11|欄目:Swift|點擊: 次

前言

Swift 支持靜態(tài)庫打包已經(jīng)有一段時間了,CocoaPods 也提供了 static_framework 參數(shù)。然而大部分的第三方依賴都沒有及時更新。

本文給出個相對方便一些的方案用上靜態(tài)庫,個人感覺在依賴不那么復(fù)雜的時候成本還是比較低的。

效果如下:

示例地址:UseStaticFramework

想辦法為每一個 pod 添加 static_framework 是關(guān)鍵。

直接修改 podspec 不太現(xiàn)實,因為 CocoaPods 并沒有提供相關(guān)的接口。但是當(dāng)一個 pod 指定 podspec 地址時,這個 podspec 會被保存在本地。

如果 pod 沒有更新,pod install 將直接從本地獲取 pod 信息,這就為我們修改 pod 提供了可能。

target 'UseStaticFramework' do
 pod 'RxSwift', :git => 'https://github.com/ReactiveX/RxSwift.git'
end

pre_install do |installer|
 installer.sandbox.specifications_root.children.each do |podspec|
  if podspec.extname() == '.json'
   edit_pod_spec podspec
  end
 end
end

def edit_pod_spec(file)
 code = File.read(file)
 json = JSON.parse(code)
 json['static_framework'] = true
 File.write(file, JSON.generate(json))
end

在 Podfile 中添加以上代碼,執(zhí)行兩次 bundle exec pod install 即可將依賴 RxSwift 變成靜態(tài)庫。相比單獨(dú)建一個 Specs 方便很多了,特別是在 RxSwift 有更新時,我們也無需增加成本,執(zhí)行 bundle exec pod update 即可。

有些依賴稍微麻煩些,比如 RxCocoa 。就目前來看,Swift 靜態(tài)庫似乎還不能混編,好在 RxCocoa 支持 SPM,在 SPM 中有一個 RxCocoaRuntime 依賴。

創(chuàng)建一個 RxCocoaRuntime.podspec 使用,再調(diào)整一下 RxCocoa 的 podspec 即可,注意添加 SWIFT_PACKAGE 編譯標(biāo)記:

pod 'RxCocoa', :git => 'https://github.com/ReactiveX/RxSwift.git'
pod 'RxCocoaRuntime', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/RxCocoaRuntime.podspec'
def edit_pod_spec(file)
 code = File.read(file)
 json = JSON.parse(code)
 json['static_framework'] = true
 if json['name'] == 'RxCocoa'
  json['xcconfig'] = {
   :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"'
  }
  json['source_files'] = ['RxCocoa/RxCocoa.swift', 'RxCocoa/Common/**/*.{swift}', 'RxCocoa/Traits/**/*.{swift}', 'RxCocoa/Foundation/**/*.{swift}', 'RxCocoa/Runtime/**/*.{swift}', 'Platform/**/*.swift']
  json['preserve_paths'] = ['RxCocoa/RxCocoa.h', 'RxCocoa/*.swift', 'RxCocoa/Common/**/*.{swift,h,m}', 'RxCocoa/Traits/**/*.{swift,h,m}', 'RxCocoa/Foundation/**/*.{swift,h,m}', 'RxCocoa/Runtime/**/*.{swift,h,m}', 'Platform/**/*.swift']
  json['dependencies'] = {
   :RxSwift => '~> 4.1',
   :RxCocoaRuntime => '~> 4.1'
  }
 end
 File.write(file, JSON.generate(json))
end

執(zhí)行兩次 bundle exec pod install,完成。

Apollo 這種也能搞,稍微麻煩一些,有些代碼沒有引入 UIKit,最終導(dǎo)致按照上面的方案編譯不過去。

pod 'SQLite.swift', :git => 'https://github.com/stephencelis/SQLite.swift.git'
pod 'SQLiteObjc', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/SQLiteObjc.podspec'

pod 'Apollo', :git => 'https://github.com/apollographql/apollo-ios.git'
pod 'Apollo/SQLite', :git => 'https://github.com/apollographql/apollo-ios.git'
# edit_pod_spec
if json['name'] == 'SQLite.swift'
 json['xcconfig'] = {
  :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"'
 }
 json['dependencies'] = {
  :SQLiteObjc => '~> 0.11.4'
 }
 json['subspecs'] = [{
  :name => 'standard',
  :source_files => 'Sources/{SQLite,SQLiteObjc}/**/*.{swift}',
  :exclude_files => 'Sources/**/Cipher.swift',
  :library => 'sqlite3'
 }]
end

post_install do |installer|
 %w(Pods/Apollo/Sources/ApolloSQLite/*.swift).flat_map { |x| Dir.glob(x) }.each do |file|
 code = File.read(file)
 unless code.include? "import UIKit"
  FileUtils.chmod("+w", file)
  File.write(file, "import UIKit\n" + code)
 end
 end
end

給這些沒添加 import UIKit 代碼補(bǔ)上就行了。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。

上一篇:Objective-C和Swift的轉(zhuǎn)換速查手冊(推薦)

欄    目:Swift

下一篇:swift 4自定義UITableCell的方法示例

本文標(biāo)題:如何快速用上Swift靜態(tài)庫詳解

本文地址:http://mengdiqiu.com.cn/a1/Swift/11944.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有