開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)第三方框架使用 (上)git
開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(二)第三方框架使用 (中)github
@[TOC]算法
這篇博客是上篇博客「開源項目分析(SwiftHub)Rxswift + MVVM + Moya 架構分析(一)第三方框架使用」 的續集,因爲篇幅過程,拆成幾部分了。spring
先回顧一下第三方框架圖:json
它提供了一些預約義的動畫綁定,並提供了一種靈活的機制,讓您能夠添加本身的預約義動畫,並在與RxCocoa綁定時使用它們。swift
當與RxCocoa綁定值時,你能夠這樣寫:數組
textObservable
.bind(to: labelFlip.rx.text)
複製代碼
每當observable發出一個新的字符串值時,它都會更新標籤的文本。但這是忽然發生的,沒有任何過渡。使用RxAnimated,你可使用animated擴展來綁定值和動畫,就像這樣:瀏覽器
textObservable
.bind(animated: labelFlip.rx.animated.flip(.top, duration: 0.33).text)
複製代碼
「不一樣之處在於」您使用bind(animated:)
而不是bind(to:)
,而後您插入animated.flip(.top, duration: 0.33)
(或其餘提供或自定義動畫方法之一)之間的rx和屬性接收器你想使用。緩存
UIView.rx.animated...isHidden
UIView.rx.animated...alpha
UILabel.rx.animated...text
UILabel.rx.animated...attributedText
UIControl.rx.animated...isEnabled
UIControl.rx.animated...isSelected
UIButton.rx.animated...title
UIButton.rx.animated...image
UIButton.rx.animated...backgroundImage
UIImageView.rx.animated...image
NSLayoutConstraint.rx.animated...constant
NSLayoutConstraint.rx.animated...isActive
複製代碼
UIView.rx.animated.fade(duration: TimeInterval)
UIView.rx.animated.flip(FlipDirection, duration: TimeInterval)
UIView.rx.animated.tick(FlipDirection, duration: TimeInterval)
UIView.rx.animated.animation(duration: TimeInterval, animations: ()->Void)
NSLayoutConstraint.rx.animated.layout(duration: TimeInterval)
複製代碼
您能夠輕鬆地添加自定義綁定動畫來匹配應用程序的視覺風格。安全
// This is your class `UILabel`
extension AnimatedSink where Base: UILabel {
// This is your property name `text` and value type `String`
public var text: Binder<String> {
let animation = self.type!
return Binder(self.base) { label, text in
animation.animate(view: label, block: {
guard let label = label as? UILabel else { return }
// Here you update the property
label.text = text
})
}
}
}
複製代碼
// This is your class `UIView`
extension AnimatedSink where Base: UIView {
// This is your animation name `tick`
public func tick(_ direction: FlipDirection = .right, duration: TimeInterval) -> AnimatedSink<Base> {
// use one of the animation types and provide `setup` and `animation` blocks
let type = AnimationType<Base>(type: RxAnimationType.spring(damping: 0.33, velocity: 0), duration: duration, setup: { view in
view.alpha = 0
view.transform = CGAffineTransform(rotationAngle: direction == .right ? -0.3 : 0.3)
}, animations: { view in
view.alpha = 1
view.transform = CGAffineTransform.identity
})
//return AnimatedSink
return AnimatedSink<Base>(base: self.base, type: type)
}
}
複製代碼
UIImageView.rx.image
以下imageObservable
.bind(to: imageView.rx.image)
複製代碼
結果是非動畫綁定的效果:
若是你使用你的新自定義動畫綁定像這樣:
imageObservable
.bind(to: imageView.rx.animated.tick(.right, duration: 0.33).image)
複製代碼
修改後的效果是這樣的:
若是你在UILabel上使用相同的動畫:
textObservable
.bind(to: labelCustom.rx.animated.tick(.left, duration: 0.75).text)
複製代碼
效果以下:
pod "RxAnimated"
複製代碼
url
會話的網絡或本地提供的數據加載圖像。UIImageView
, NSImageView
, NSButton
和UIButton
的視圖擴展來直接從URL
設置圖像。SwiftUI
支持。let url = URL(string: "https://example.com/image.png")
imageView.kf.setImage(with: url)
複製代碼
Kingfisher將從url
下載圖像,將其發送到內存緩存和磁盤緩存,並在imageView
中顯示。當您稍後使用相同的URL
設置時,圖像將從緩存中檢索並當即顯示。
若是你使用SwiftUI也能夠這樣寫:
import KingfisherSwiftUI
var body: some View {
KFImage(URL(string: "https://example.com/image.png")!)
}
複製代碼
此外,Kingfisher還提供了一些高階用法,用於解決複雜的問題,有了這些強大的選項,您能夠用簡單的方式用Kingfisher完成困難的任務。 例如,下面的代碼:
let url = URL(string: "https://example.com/high_resolution_image.png")
let processor = DownsamplingImageProcessor(size: imageView.bounds.size)
>> RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.indicatorType = .activity
imageView.kf.setImage(
with: url,
placeholder: UIImage(named: "placeholderImage"),
options: [
.processor(processor),
.scaleFactor(UIScreen.main.scale),
.transition(.fade(1)),
.cacheOriginalImage
])
{
result in
switch result {
case .success(let value):
print("Task done for: \(value.source.url?.absoluteString ?? "")")
case .failure(let error):
print("Job failed: \(error.localizedDescription)")
}
}
複製代碼
上面代碼作了這些操做:
- 下載高分辨率圖像。
- 向下採樣以匹配圖像視圖的大小。
- 使它在給定的半徑內成爲一個圓角。
- 下載時顯示系統指示符和佔位符圖像。
- 準備好後,它會用「漸入淡出」效果使小的縮略圖產生動畫效果。
- 原始的大圖也被緩存到磁盤供之後使用,以免在詳細視圖中再次下載它。
- 當任務完成時,不管是成功仍是失敗,都會打印控制檯日誌。
func clearCache() {
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
}
複製代碼
//顯示菊花
imageView.kf.indicatorType = .activity
imageView.kf.setImage(with: url, placeholder: nil, options: [.transition(ImageTransition.fade(1))], progressBlock: { (receviveeSize, totalSize) in
print("\(receviveeSize)/\(totalSize)")
}) { (image, error, cacheType, imageURL) in
print("Finished")
// 加載完成的回調
// image: Image? `nil` means failed
// error: NSError? non-`nil` means failed
// cacheType: CacheType
// .none - Just downloaded
// .memory - Got from memory cache
// .disk - Got from disk cache
// imageUrl: URL of the image
}
複製代碼
imageView.kf.indicatorType = .activity
imageView.kf.setImage(with: url)
//使用本身的gif圖片做爲下載指示器
let path = Bundle.main.path(forResource: "loader", ofType: "gif")!
let data = try! Data(contentsOf: URL(fileURLWithPath: path)) imageView.kf.indicatorType = .image(imageData: data)
imageView.kf.setImage(with: url)
複製代碼
struct KYLIndicator: Indicator {
let view: UIView = UIView()
func startAnimatingView() {
view.isHidden = false
}
func stopAnimatingView() {
view.isHidden = true
}
init() {
view.backgroundColor = .red
}
}
let indicator = KYLIndicator()
imageView.kf.indicatorType = .custom(indicator: indicator)
複製代碼
imageView.kf.setImage(with: url, options: [.transition(.fade(0.2))])
複製代碼
let processor = RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
複製代碼
let uiButton: UIButton = UIButton()
uiButton.kf.setImage(with: url, for: .normal, placeholder: nil, options: nil, progressBlock: nil, completionHandler: nil)
uiButton.kf.setBackgroundImage(with: url, for: .normal, placeholder: nil, options: nil, progressBlock: nil, completionHandler: nil)
複製代碼
// 設置磁盤緩存大小
// Default value is 0, which means no limit.
// 50 MB
ImageCache.default.maxDiskCacheSize = 50 * 1024 * 1024
複製代碼
// 設置緩存過時時間
// Default value is 60 * 60 * 24 * 7, which means 1 week.
// 3 days
ImageCache.default.maxCachePeriodInSecond = 60 * 60 * 24 * 3
複製代碼
// Default value is 15.
// 30 second
ImageDownloader.default.downloadTimeout = 30.0
複製代碼
其餘設置相關
// 設置磁盤緩存大小
// Default value is 0, which means no limit.
// 50 MB
ImageCache.default.maxDiskCacheSize = 50 * 1024 * 1024
// 獲取緩存磁盤使用大小
ImageCache.default.calculateDiskCacheSize { size in
print("Used disk size by bytes: \(size)")
}
// 設置緩存過時時間
// Default value is 60 * 60 * 24 * 7, which means 1 week.
// 3 days
ImageCache.default.maxCachePeriodInSecond = 60 * 60 * 24 * 3
// 設置超時時間
// Default value is 15.
// 30 second
ImageDownloader.default.downloadTimeout = 30.0
// Clear cache manually
// Clear memory cache right away.
cache.clearMemoryCache()
// Clear disk cache. This is an async operation.
cache.clearDiskCache()
// Clean expired or size exceeded disk cache. This is an async operation.
cache.cleanExpiredDiskCache()
複製代碼
imageView.kf.setImage(with: url, options: [.forceRefresh])
複製代碼
let resource = ImageResource(downloadURL: url!, cacheKey: "kyl_cache_key")
imageView.kf.setImage(with: resource)
複製代碼
Kingfisher 主要由兩部分組成,
ImageDownloader
用於管理下載;ImageCache
用於管理緩存,你能夠單獨使用其中一個.
//使用ImageDownloader下載圖片
ImageDownloader.default.downloadImage(with: url!, options: [], progressBlock: nil) { (image, error, url, data) in
print("Downloaded Image: \(image)")
}
// 使用ImageCache緩存圖片
let image: UIImage = UIImage(named: "xx.png")!
ImageCache.default.store(image, forKey: "key_for_image")
// Remove a cached image
// From both memory and disk
ImageCache.default.removeImage(forKey: "key_for_image")
// Only from memory
ImageCache.default.removeImage(forKey: "key_for_image",fromDisk: false)
複製代碼
Downloader
和cache
代替默認的let kyldownloader = ImageDownloader(name: "kongyulu_image_downloader")
kyldownloader.downloadTimeout = 150.0
let cache = ImageCache(name: "kyl_longer_cache")
cache.maxDiskCacheSize = 60 * 60 * 24 * 30
imageView.kf.setImage(with: url, options: [.downloader(kyldownloader), .targetCache(cache)])
// 取消下載
imageView.kf.cancelDownloadTask()
複製代碼
// MARK:- 下載圖片
imageView.kf.indicatorType = .activity
let cachePath = ImageCache.default.cachePath(forKey: PhotoConfig.init().cachePath)
guard let path = (try? ImageCache.init(name: "cameraPath", cacheDirectoryURL: URL(fileURLWithPath: cachePath))) ?? nil else { return }
imageView.kf.setImage(with: URL(string: smallUrlStr), placeholder:UIImage(named: "PhotoRectangle") , options: [.targetCache(path)], progressBlock: { (receivedData, totolData) in
// 這裏用進度條或者繪製view均可以,而後根據 percentage% 表示進度就好了
//let percentage = (Float(receivedData) / Float(totolData)) * 100.0
//print("downloading progress is: \(percentage)%")
}) { result in
// switch result {
//
// case .success(let imageResult):
// print(imageResult)
//
// case .failure(let aError):
// print(aError)
// }
}
複製代碼
let urls = ["http://www.baidu.com/image1.jpg", "http://www.baidu.com/image2.jpg"]
.map { URL(string: $0)! }
let prefetcher = ImagePrefetcher(urls: urls) {
skippedResources, failedResources, completedResources in
print("These resources are prefetched: \(completedResources)")
}
prefetcher.start()
// Later when you need to display these images:
imageView.kf.setImage(with: urls[0])
anotherImageView.kf.setImage(with: urls[1])
複製代碼
可能有些朋友不太熟悉HTTPS握手的過程,要理解證書認證機制,有必要理解一下HTTPS握手過程:
發送HTTPS請求首先要進行SSL/TLS握手,握手過程大體以下:
- 客戶端發起握手請求,攜帶隨機數、支持算法列表等參數。
- 服務端收到請求,選擇合適的算法,下發公鑰證書和隨機數。
- 客戶端對服務端證書進行校驗,併發送隨機數信息,該信息使用公鑰加密。
- 服務端經過私鑰獲取隨機數信息。
- 雙方根據以上交互的信息生成session ticket,用做該鏈接後續數據傳輸的加密密鑰。
第3步中,客戶端須要驗證服務端下發的證書,驗證過程有如下兩個要點:
- 客戶端用本地保存的根證書解開證書鏈,確認服務端下發的證書是由可信任的機構頒發的。
- 客戶端須要檢查證書的domain域和擴展域,看是否包含本次請求的host。 若是上述兩點都校驗經過,就證實當前的服務端是可信任的,不然就是不可信任,應當中斷當前鏈接。
當客戶端直接使用IP地址發起請求時,請求URL
中的host
會被替換成HTTP
DNS
解析出來的IP,因此在證書驗證的第2步,會出現domain
不匹配的狀況,致使SSL/TLS握手不成功。
更多詳情請參考我以前寫的一篇關於HTTPS自簽名證書上傳下載文件的博客:
IOS 網絡協議(一) 自簽名證書HTTPS文件上傳下載(上)
以下圖:
過程詳解: ![]()
- ①客戶端的瀏覽器向服務器發送請求,並傳送客戶端SSL 協議的版本號,加密算法的種類,產生的隨機數,以及其餘服務器和客戶端之間通信所須要的各類信息。
- ②服務器向客戶端傳送SSL 協議的版本號,加密算法的種類,隨機數以及其餘相關信息,同時服務器還將向客戶端傳送本身的證書。
- ③客戶端利用服務器傳過來的信息驗證服務器的合法性,服務器的合法性包括:證書是否過時,發行服務器證書的CA 是否可靠,發行者證書的公鑰可否正確解開服務器證書的「發行者的數字簽名」,服務器證書上的域名是否和服務器的實際域名相匹配。若是合法性驗證沒有經過,通信將斷開;若是合法性驗證經過,將繼續進行第四步。
- ④用戶端隨機產生一個用於通信的「對稱密碼」,而後用服務器的公鑰(服務器的公鑰從步驟②中的服務器的證書中得到)對其加密,而後將加密後的「預主密碼」傳給服務器。
- ⑤若是服務器要求客戶的身份認證(在握手過程當中爲可選),用戶能夠創建一個隨機數而後對其進行數據簽名,將這個含有簽名的隨機數和客戶本身的證書以及加密過的「預主密碼」一塊兒傳給服務器。
- ⑥若是服務器要求客戶的身份認證,服務器必須檢驗客戶證書和簽名隨機數的合法性,具體的合法性驗證過程包括:客戶的證書使用日期是否有效,爲客戶提供證書的CA 是否可靠,發行CA 的公鑰可否正確解開客戶證書的發行CA 的數字簽名,檢查客戶的證書是否在證書廢止列表(CRL)中。檢驗若是沒有經過,通信馬上中斷;若是驗證經過,服務器將用本身的私鑰解開加密的「預主密碼」,而後執行一系列步驟來產生主通信密碼(客戶端也將經過一樣的方法產生相同的主通信密碼)。
- ⑦服務器和客戶端用相同的主密碼即「通話密碼」,一個對稱密鑰用於SSL 協議的安全數據通信的加解密通信。同時在SSL 通信過程當中還要完成數據通信的完整性,防止數據通信中的任何變化。
- ⑧客戶端向服務器端發出信息,指明後面的數據通信將使用的步驟. ⑦中的主密碼爲對稱密鑰,同時通知服務器客戶端的握手過程結束。
- ⑨服務器向客戶端發出信息,指明後面的數據通信將使用的步驟⑦中的主密碼爲對稱密鑰,同時通知客戶端服務器端的握手過程結束。
- ⑩SSL 的握手部分結束,SSL 安全通道的數據通信開始,客戶和服務器開始使用相同的對稱密鑰進行數據通信,同時進行通信完整性的檢驗。
//取出downloader單例
let downloader = KingfisherManager.shared.downloader
//信任ip爲106的Server,這裏傳入的是一個數組,能夠信任多個IP
downloader.trustedHosts = Set(["192.168.1.106"])
//使用KingFisher給ImageView賦網絡圖片
iconView.kf.setImage(with: iconUrl)
複製代碼
安裝環境要求: iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+ Swift 4.0+
Pod install
pod 'Kingfisher'
複製代碼
後續補充...
R.swift 在Swift項目中得到強類型、自動完成的資源,如圖像、字體和segue。
R.swift 使你的代碼使用資源具備以下特性:
- 全類型,較少類型轉換和猜想方法將返回什麼
- 編譯時檢查,沒有更多的不正確的字符串,使您的應用程序崩潰在運行時
- 自動完成,永遠沒必要再猜圖像的名字
例如,使用R.swift 以前你可能會這樣寫你的代碼:
let icon = UIImage(named: "settings-icon")
let font = UIFont(name: "San Francisco", size: 42)
let color = UIColor(named: "indicator highlight")
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")
複製代碼
使用R.swift 以後,你能夠這樣寫代碼:
let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let color = R.color.indicatorHighlight()
let viewController = CustomViewController(nib: R.nib.customView)
let string = R.string.localizable.welcomeWithName("Arthur Dent")
複製代碼
這裏有官方提供的Demo:Examples , 在realm中使用
看看自動填充的效果多酷:
自動完成圖片:
安裝R.swift 到您的項目後,您可使用R-struct
訪問資源。若是結構是過期的,只是創建和R.swift 將糾正任何失蹤/改變/增長的資源。
R.swift 目前支持這些類型的資源:
沒有使用R.swift 這樣訪問圖片
let settingsIcon = UIImage(named: "settings-icon")
let gradientBackground = UIImage(named: "gradient.jpg")
複製代碼
使用R.swift 後這樣訪問:
let settingsIcon = R.image.settingsIcon()
let gradientBackground = R.image.gradientJpg()
複製代碼
此外R.swift 還支持文件夾中分組的方式:
選擇「提供名稱空間」分組資產結果:
let image = R.image.menu.icons.first()
複製代碼
沒有使用R.swift 這樣訪問:
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
複製代碼
使用R.swift 後這樣訪問:
let lightFontTitle = R.font.acmeLight(size: 22)
複製代碼
提示:系統字體也須要這個嗎? 看一下UIFontComplete庫,它有一個相似的解決方案,用於蘋果公司發佈的iOS字體。
沒有使用R.swift 這樣訪問:
let jsonURL = Bundle.main.url(forResource: "seed-data", withExtension: "json")
let jsonPath = Bundle.main.path(forResource: "seed-data", ofType: "json")
複製代碼
使用R.swift 後這樣訪問:
let jsonURL = R.file.seedDataJson()
let jsonPath = R.file.seedDataJson.path()
複製代碼
沒有使用R.swift 這樣訪問:
view.backgroundColor = UIColor(named: "primary background")
複製代碼
使用R.swift 後這樣訪問:
view.backgroundColor = R.color.primaryBackground()
複製代碼
沒有使用R.swift 這樣訪問:
let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
// Formatted strings
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
// Stringsdict files
let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
複製代碼
使用R.swift 後這樣訪問:
// Localized strings are grouped per table (.strings file)
let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()
// Functions with parameters are generated for format strings
let welcomeName = R.string.localizable.welcomeWithName("Alice")
// Functions with named argument labels are generated for stringsdict keys
let progress = R.string.localizable.copyProgress(completed: 4, total: 23)
複製代碼
沒有使用R.swift 這樣訪問:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsController
複製代碼
使用R.swift 後這樣訪問:
let storyboard = R.storyboard.main()
let initialTabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()
複製代碼
沒有使用R.swift 這樣訪問:
// Trigger segue with:
performSegue(withIdentifier: "openSettings", sender: self)
// And then prepare it:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let settingsController = segue.destination as? SettingsController,
let segue = segue as? CustomSettingsSegue, segue.identifier == "openSettings" {
segue.animationType = .LockAnimation
settingsController.lockSettings = true
}
}
複製代碼
使用R.swift 後這樣訪問:
// Trigger segue with:
performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)
// And then prepare it:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
typedInfo.segue.animationType = .LockAnimation
typedInfo.destinationViewController.lockSettings = true
}
}
複製代碼
提示:看看SegueManager庫,它使segues塊爲基礎,並與r.s ft兼容。
沒有使用R.swift 這樣訪問:
let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiate(withOwner: nil, options: nil)
let customView = rootViews[0] as? CustomView
let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)
複製代碼
使用R.swift 後這樣訪問:
let nameOfNib = R.nib.customView.name
let customViewNib = R.nib.customView()
let rootViews = R.nib.customView.instantiate(withOwner: nil)
let customView = R.nib.customView.firstView(owner: nil)
let viewControllerWithNib = CustomViewController(nib: R.nib.customView)
複製代碼
(1) 重用TableViewCell
沒有使用R.swift 這樣訪問:
class FaqAnswerController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textCellNib = UINib(nibName: "TextCell", bundle: nil)
tableView.register(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCell(withIdentifier: "TextCellIdentifier", for: indexPath) as! TextCell
textCell.mainLabel.text = "Hello World"
return textCell
}
}
複製代碼
使用R.swift 後這樣訪問:
在可重用單元格界面生成器「屬性」檢查面板上,將單元格「標識符」字段設置爲要註冊和退出隊列的相同值。
class FaqAnswerController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(R.nib.textCell)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.textCell, for: indexPath)!
textCell.mainLabel.text = "Hello World"
return textCell
}
}
複製代碼
(2) 重用CollectionViewCell
沒有使用R.swift 這樣訪問:
class RecentsController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let talkCellNib = UINib(nibName: "TalkCell", bundle: nil)
collectionView?.register(talkCellNib, forCellWithReuseIdentifier: "TalkCellIdentifier")
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TalkCellIdentifier", for: indexPath) as! TalkCell
cell.configureCell("Item \(indexPath.item)")
return cell
}
}
複製代碼
使用R.swift 後這樣訪問:
在可重用單元格界面生成器「屬性」檢查面板上,將單元格「標識符」字段設置爲要註冊和退出隊列的相同值。
class RecentsController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(R.nib.talkCell)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.talkCell, for: indexPath)!
cell.configureCell("Item \(indexPath.item)")
return cell
}
}
複製代碼
CocoaPods是推薦的安裝方式,由於這樣能夠避免在項目中包含任何二進制文件。
注意:R.swift 是一個用於構建步驟的工具,它不是一個動態庫。所以,它不可能安裝Carthage。
CocoaPods(推薦) 安裝步驟:
- 添加
pod 'R.swift'
到您的Podfile和運行pod install
- 在Xcode中:單擊文件列表中的項目,在「目標」下選擇目標,單擊「構建階段」選項卡,經過單擊左上角的小加號圖標添加新的運行腳本階段
- 將新的運行腳本階段拖動到編譯源階段之上,並在檢查pod清單之下。鎖定,展開並粘貼如下腳本:
"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"
- 將
$TEMP_DIR/rswift-lastrun
添加到「輸入文件」中,將$SRCROOT/R.generated.swift
添加到構建階段的「輸出文件」中- 創建你的項目,在Finder中你會看到一個
R.generated.swift
在$SRCROOT
文件夾中,拖動R.generated.swift
文件到你的項目中,若是須要,取消勾選Copy項