Swift - Optional Protocol Implementation

In my previous blog, I explained the Protocol and optional protocol in a generic way.

Now, we will dive deep into Protocol implementation and may be this is how the UITableViewDataSource and UITableViewDelegate would have been implemented.

Objective's of this document would be to understand the optional protocol following concepts
1) Check implementation of the optional method by writing ? , after the name of the method.
2) When a method is optional it's type automatically becomes as optional. example
type (Int) -> String becomes (Int) -> String?

_________________________________________________________________________________


Lets start with a controller implementation , listed in the below code. There will no UI re-presentation , the protocol explanation is solely for simple understanding.


1) ProfileViewsDelegate.swift.
We will be implementing the protocol for the ProfileViews.swift.

import Foundation
import UIKit

@objc protocol ProfileViewsDelegate {
    
    //Mandate
    func profileViewsNumberToDisplay() -> Int
    
    //Optional
    @objc optional func profileViewsBackgroundColorAt(index:Int) -> UIColor
    

}


2) CustomViewController.swift can be assigned as Controller in storyboard.
If we check the code , I have commented profileViewsBackgroundColorAt, to show that its a optional requirement and does not have to implemented.

import UIKit

class CustomViewController: UIViewController,ProfileViewsDelegate {

    lazy var profileViewsObject:ProfileViews = ProfileViews(profileViewsDelegate: self)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        profileViewsObject.loadAllProfiles()
    }
    
    
    func profileViewsNumberToDisplay() -> Int {
        return 10
    }
    
//    func profileViewsBackgroundColorAt(index: Int) -> UIColor {
//        return UIColor.red
//    }


}







3) ProfileViews.swift
It will load all profile's , for which we have to implement protocol ProfileViewsDelegate.
If you check the ProfileViewsDelegate , the protocol has one requirement as mandate and other one as optional.

import Foundation
import UIKit

class ProfileViews: UIView {
    
    var profileViewsDelegate:ProfileViewsDelegate?
    
    init() {
        super.init(frame: .zero)
    }
    
    convenience init(profileViewsDelegate:ProfileViewsDelegate){
        self.init()
        self.profileViewsDelegate = profileViewsDelegate
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //Load All Profiles
    func loadAllProfiles(){
        
        if let delegate = self.profileViewsDelegate{
            let itemCount = delegate.profileViewsNumberToDisplay()
            for i in 0..<itemCount {
                if let color = delegate.profileViewsBackgroundColorAt?(index: i) {
                    print("Color at index \(i)=\(color)"   
                }
                
                
            }
        }
        

    }
    
    

}


The coding of ProfileViews help's us to learn the two objective's of this document. 

1)  To check whether the class adopts to the optional requirement we use "?".
i.e delegate.profileViewsBackgroundColorAt?

2) The second one is when a method is optional the type also becomes optional.

i.e we have to unwrap the delegate.profileViewsBackgroundColorAt?(index: i) to color Object.

           if let color = delegate.profileViewsBackgroundColorAt?(index: i) {
                print("Color at index \(i)=\(color)"   
           }


If the method was not optional we would have written it as simply.

let color = delegate.profileViewsBackgroundColorAt?(index: i) 
print("Color at index \(i)=\(color)"   





Comments

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

Custom Editing Control for UITableViewCell , like iPhone Mail Appliation