Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Mandatory and optional methods in protocols

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 395
    Comment on it

    The methods and functions of the protocols can be categorized as, required or optional. The difference between these two specifiers is that, the programmer may or may not use the optional methods declared in the protocol, or may use some of them and leave the rest unused. But it is mandatory to use and implement all the functions and methods declared under the @required specifier.

    The syntax for doing it is as follows.

    @protocol Myprotocol <NSObject>
    
    @required
    -(void)firstRequiredMethod;
    -(void)secondRequiredMethod;
    
    @optional
    -(void)firstOptionalMethod;
    -(void)secondOptionalMethod;
    -(void)thirdOptionalMethod;
    

    In the above example, the programmer have to impliment the firstRequiredMethod as well as the secondRequiredMethod. But it is not necessary to use all of the optional methods. The programmer may use all of them or may not use any of them, depending on the requirement of the application.

    for example: When the programmer uses UITableViewDataSource methods, he may or may not use :

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
    

    Therefore these are the Optional methods of the table view.

    But the programmer must implement the following methods by all means.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    PS. If any method is declared without any specifier(required or optional), it is considered required by default, and the programmer have to implement them.

 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: