Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Protocols in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 500
    Comment on it

    Hi All,

    Protocol : - A protocol defines a representation of methods and properties that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

    Protocols are of two type, required and optional. Required protocols are mandatory to be implemented by a class, structure, or enumeration who conform to protocol. On the other hand, Optional protocols are not mandatory but they are called if implemented in a class. 

    For example :- In this example , We are going to implement custom protocols as follow

    • Make a class in which you need protocols, Like :-
    @objc public protocol MySchoolDelegate : NSObjectProtocol {
        
        // Display customization
        func showAllObject(obj:NSString)
        
        optional func optionalDelegateMethod(obj:NSString)
        
       
    }
    class MySchool: NSObject {
        var delegate:MySchoolDelegate?
        required override init() {
            // initializer implementation goes here
            super.init()
        }
        
        func anyMethodCalculating(obj:NSString){
            delegate?.showAllObject(obj)
            if obj.isEqualToString("REST") {
                delegate?.optionalDelegateMethod?(obj)
            }
        }
        
    }

    In the above code , showAllObject() is required method and optionalDelegateMethod() is an optional method. so when you make an object of class MySchool and set object delegate to class then you have to implement showAllObject() method in that class.

    • Use of protocols ,Like:-
     override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let obj = MySchool()
            obj.delegate = self;
            obj.anyMethodCalculating("tesing here")
            obj.anyMethodCalculating("REST")
        }
        func showAllObject(obj:NSString){
            print("Delegate function called = ",obj)
        }
        func optionalDelegateMethod(obj:NSString){
            print("Delegate Optional function called = ",obj)
        }

     Above code is from viewController. Now you can check the result by running above code and you console will show you like :-

    Delegate function called =  tesing here
    Delegate function called =  REST
    Delegate Optional function called =  REST

    as you can see mandatory method is called two times and and optional is called one time only (when you passed string REST ).

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: