[Objective-c] 006_protocol (protocol)

Source: Internet
Author: User



Learn Java students know Interface (interface), then there is no interface in the objective-c? In fact objective-c with protocol (protocol) to achieve, in objective-c specific how to use, we look directly at the code example.



Studentprotocol


///////////////////// .h ///////////////////////
  
#import <Foundation / Foundation.h>

@protocol StudentProtocol <NSObject>

@optional // The following method is optional
-(void) fallInLove: (NSString *) name;

@required // The following methods must be implemented
-(void) curriculum;

@end


Studentprotocol has written how to use it, we take the student class as an example, student to achieve studentprotocol only in the Student.h class name after adding <studentprotocol> STUDENT.M implement the methods defined in the Studentprotocol.


/////////////////.h //////////////////////

#import "Person.h"
#import "StudentProtocol.h"

@interface Student: Person <StudentProtocol>

-(id) initWithName: (NSString *) name sex: (NSString *) sex age: (int) age;
@end


///////////////// .m /////////////////////

#import "Student.h"

@implementation Student

-(id) initWithName: (NSString *) name sex: (NSString *) sex age: (int) age
{
    self = [super init];
    if (self) {
        self.name = name;
        self.sex = sex;
        self.age = age;
    }
    return self;
}

-(Person *) work
{
    NSLog (@ "% @ is working", self.name);
    return 0;
}

-(void) printInfo {
    NSLog (@ "My name is:% @ This year is% d years old. I am a% @% @", self.name, self.age, self.sex, NSStringFromClass ([self class]));
}

#pragma mark StudentProtocol

-(void) fallInLove: (NSString *) name {
    NSLog (@ "I'm still a student, I can talk about love or not ... but I'm still in love with --->% @ <---" Love, "name);
}

-(void) curriculum {
    NSLog (@ "I am a student and must study in class ...");
}

@end


In Studentprotocol declaration of two methods, there is an optional implementation, then we have no way to know the Student to achieve this method? Yes, keep looking at the code.


#import "AppDelegate.h"
#import "Student.h"
#import "StudentProtocol.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
    
     Student * s = [[Student alloc] initWithName: @ "xiaoming" sex: @ "male" age: 12];
     if ([s conformsToProtocol: @protocol (StudentProtocol)]) {// determine if a protocol is followed
         NSLog (@ "I followed --- StudentProtocol --- protocol");
         if ([s respondsToSelector: @selector (fallInLove :)]) {// Judge if there is a method
             [s fallInLove: @ "ruhua"];
         }
     }

     return YES;
}

@end


Test results:



2015-06-14 18:23:13.104 attendance[16464:617950] I followed ---studentprotocol--- protocol .



2015-06-14 18:23:13.104 attendance[16464:617950] I am still a student , talk About love can ..., but I still ---> like flowers <--- fall in love.











This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4575504.html








[Objective-c] 006_protocol (protocol)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.