http://blog.ios-developers.io/vIEw-controller-extensions/
Say you are trying to make an iOS app that uses aUItableVIEw
. Along with the code that provIDes custom functionality,your vIEw controller will need to conform to theUItableVIEw
data source and delegate in order to access methods that bring your vIEws to life. This means including all of the protocol methods in your vIEw controller.
// VIEwController.swiftclass VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Extra vIEw initializations go here... } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() } // DataSource func numberOfSectionsIntableVIEw(tablevIEw: UItableVIEw) -> Int { return 1 } func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { return Data.dataStore.count } func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { let cell = tableVIEw.dequeueReusableCellWithIDentifIEr("Cell",forIndexPath: indexPath) let row = indexPath.row cell.textLabel?.text = Data.dataStore[row] return cell } // Delegate func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { let row = indexPath.row print(Data.dataStore[row]) }}
Phew,there's a lot of code cluttering up the vIEw controller,and it's really just there to conform to the protocols! Of course you can always just use some//colorful
comments orMARK
organization in order to add visual separation from the rest of your code. But what if there was a way to just move all of that code into its own file? Good news everyone,there is! All we need are some handyextensions
.
Extensions allow you to extend the functionality of a class,structure,enumeration,or protocol type. Say you wanted to add a custom method to the Nsstring class. Extensions let you do that. Or maybe you want to separate out some code,say in a vIEw controller,into a different file. Extensions let you do that! Just take the code from before and bring it out into it's own file. This is demonstrated as follows.
VIEwController.swiftclass VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Extra vIEw initializations go here... } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() }}VIEwController+DataSource.swift
extension VIEwController: UItableVIEwDataSource { func numberOfSectionsIntableVIEw(tablevIEw: UItableVIEw) -> Int { return 1 } func tableVIEw(tableVIEw: UItableVIEw,forIndexPath: indexPath) let row = indexPath.row cell.textLabel?.text = Data.dataStore[row] return cell }}VIEwController+Delegate.swift
extension VIEwController: UItableVIEwDelegate { func tableVIEw(tableVIEw: UItableVIEw,sans-serif; Font-size:18px; letter-spacing:0.12px; line-height:31.5px"> Voilà. Now all of the code related to the DataSource and Delegate protocols are contained within their own files. These methods are isolated from the rest of your vIEw controller,yet they still provIDe the essential capabilitIEs to your program. Just look at how clean that vIEw controller is Now!One thing to make sure of though is that the delegate and data source for your tablevIEw is assigned correctly. The easIEst way to do this is through Interface Builder with the
ctrl+drag
functionality. Select the tablevIEw and connect it to the parent vIEw controller for both data source and delegate,like so...
Now you're all set. Sporting a sleek,slimmed down VC!
总结以上是内存溢出为你收集整理的swift中extension的应用全部内容,希望文章能够帮你解决swift中extension的应用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)