正文
我們?cè)趯?shí)際開發(fā)中偶爾會(huì)遇到一些很傻很天真的BUG侮叮,結(jié)果花了很長(zhǎng)時(shí)間調(diào)試采幌,最后發(fā)現(xiàn)這些BUG大多源于一個(gè)非常小的問題棘催。最常見的就是在一個(gè)字符串中敲錯(cuò)了一兩個(gè)字母累舷。在實(shí)際開發(fā)中我們又不得不面對(duì)很多讀取資源文件的操作怖竭,這些操作很多都需要直接操作資源文件名稱的字符串來完成哼审。
比如初始化Image
let image = UIImage(named: "checklist_not_completed_20x20_")
注:從Xcode8開始,讀取image可以使用Image Literal
Perform Segue
self.performSegue(withIdentifier: "showDetail", sender: self)
這樣其實(shí)有兩個(gè)問題:
- 對(duì)于字符串Xcode IDE沒有代碼提示功能,重復(fù)敲這些字符串很費(fèi)時(shí)間剪返,并且當(dāng)字符串稍微復(fù)雜點(diǎn)時(shí),你有一定的概率敲錯(cuò)泌辫,增加了出BUG的概率随夸。
- Xcode IDE也不會(huì)為字符串提供編譯時(shí)檢測(cè),所以如果敲錯(cuò)震放,你只能等測(cè)試出現(xiàn)BUG宾毒,然后去查找修復(fù),成本很高殿遂。
今天我們就來探討下怎么優(yōu)化Segue诈铛,來解決我們上面提到的問題。
public protocol CustomSegueProtocol {
associatedtype CustomSegueIdentifier: RawRepresentable
func performCustomSegue(_ segue: CustomSegueIdentifier, sender: Any?)
func customSegueIdentifier(forSegue segue: UIStoryboardSegue) -> CustomSegueIdentifier
}
首先定義一個(gè)協(xié)議CustomSegueProtocol
墨礁,在協(xié)議里添加一個(gè)關(guān)聯(lián)類型CustomSegueIdentifier
幢竹,繼承與RawRepresentable
協(xié)議。同時(shí)聲明兩個(gè)方法performCustomSegue(_::)
和customSegueIdentifier(_:)
extension CustomSegueProtocol where Self: UIViewController,
CustomSegueIdentifier.RawValue == String {
public func performCustomSegue(_ segue: CustomSegueIdentifier, sender: Any?) {
performSegue(withIdentifier: segue.rawValue, sender: sender)
}
public func customSegueIdentifier(forSegue segue: UIStoryboardSegue) -> CustomSegueIdentifier {
guard let identifier = segue.identifier,
let customSegueIndentifier = CustomSegueIdentifier(rawValue: identifier) else {
fatalError("Cannot get custom segue indetifier for segue: \(segue.identifier ?? "").")
}
return customSegueIndentifier
}
}
給CustomSegueProtocol
添加默認(rèn)實(shí)現(xiàn)恩静,并且在添加一些約束焕毫。首先我們要求CustomSegueProtocol
協(xié)議只能被UIViewController
及其子類使用。其次關(guān)聯(lián)類型CustomSegueIdentifier
的RawValue
為String
驶乾,因?yàn)槲覀冊(cè)趕toryboard里面設(shè)置Segue的identifier只能是String
邑飒。具體實(shí)現(xiàn)內(nèi)容比較簡(jiǎn)單,就是調(diào)用Apple原生處理segue的方法级乐。
下面看看具體怎么使用疙咸?
class TestCustomSegueViewController: UITableViewController, CustomSegueProtocol {
enum CustomSegueIdentifier: String {
case showNext
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performCustomSegue(.showNext, sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch customSegueIdentifier(forSegue: segue) {
case .showNext:
// Configuration for next page
}
}
}
首先讓你的UIViewController
繼承CustomSegueProtocol
協(xié)議。然后把該協(xié)議的關(guān)聯(lián)類型CustomSegueIdentifier
定義為枚舉類型风科,并列出所有的segue撒轮。然后在操作segue的時(shí)候調(diào)用performCustomSegue(_::)
和customSegueIdentifier(_:)
這兩個(gè)協(xié)議函數(shù)。
總結(jié)
-
通過上面的改造贼穆,我們用新的方式來操作segue有以下一些好處:
- Xcode 的代碼提示功能题山,減少了敲錯(cuò)字母引起B(yǎng)UG的概率
- 因?yàn)樾碌?code>CustomSegueIdentifier是個(gè)強(qiáng)類型,Xcode能在編譯時(shí)期對(duì)其進(jìn)行類型檢測(cè)故痊。
- 在
UIViewController
統(tǒng)一用枚舉類型定義所有的segue臀蛛,方便管理維護(hù),同時(shí)也可以充分利用枚舉一些特性。
具體詳細(xì)代碼請(qǐng)參考SwiftDevHints
聯(lián)系方式
備注
我們?cè)谲浖_發(fā)的過程中浊仆,為了提高效率客峭,其中很重要的一環(huán)就是把反復(fù)使用到的功能或模塊封裝起來。因此我在GitHub上開源了一個(gè)小工具集 - SwiftDevHints抡柿,來總結(jié)自己在實(shí)際項(xiàng)目開發(fā)過程中封裝的一些小功能舔琅。
剛剛介紹的只是其中一個(gè)小功能,想看看其它更多功能洲劣,請(qǐng)直接點(diǎn)擊SwiftDevHints备蚓。如果您覺得對(duì)您有所幫助,請(qǐng)給一個(gè)star吧囱稽。