Posts

Showing posts from September, 2017

Swift - Optional Protocol Implementation

Image
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 Profile

Swift Protocol and Implementation

Image
Protocols in Swift , define requirements that has to be implemented by a Class adopting or conforming to it. Protocols can be adopted by a Class , Structure or Enumeration , all of which can actually implement them based on the requirements. But, in one scenario you can't adopt protocol for Structure and Enumeration. Please go through the entire article for further understanding. Lets consider a simple example with Inheritance and Requirement (Protocol defines Requirements). Object Inheritance. Department Object is inherited by Administration , Finance and Human Resource Department. So all the departments will have inherited properties like departmentName and departmentId. Protocol. Protocol define the requirements. These requirements can be required or optional.  requirements If you notice in the design listed below, you will notice two type's for requirements. 1) Required  func dressCode(departmentType: Int ) -> UIColor func numberO