Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of organizing UIViewController class with the help of extensions
// ViewController.swift
// Created by Dejan Atanasov on 01/04/2017.
// Copyright © 2017 Dejan Atanasov. All rights reserved.
import UIKit
class ViewController: UIViewController {
//MARK: Private Properties
@IBOutlet fileprivate weak var titleLbl: UILabel!
@IBOutlet fileprivate weak var actionBtn: UIButton!
@IBOutlet fileprivate weak var collectionView: UICollectionView!
//MARK: Internal Properties
var title: String!
override func viewDidLoad() {
//MARK: Private extension - used to store private methods
private extension ViewController{
/* do any UI related stuff here */
func setupUI(){
self.title = "Some Title"
/* keep your actions in this block as well */
@IBAction func someAction(btn: UIButton){
//MARK: UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
return cell
//MARK: UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//MARK: UICollectionViewDelegateFlowLayout
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
//MARK: SomeCustomDelegate
extension ViewController: SomeCustomDelegate{
func customDelegateMethod() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment