Dark Horse Programmer--oc Foundation eighth article

Source: Internet
Author: User

Eight. OC Basics (protocal)

 Protocal Point of departure: he is only used to declare methods.

1. Can be used to declare a lot of methods (but cannot declare member variables)


2. As long as a class complies with this agreement, it is equivalent to having all the method declarations in this agreement


3. As long as the parent complies with a protocol, the subclass also adheres to the


4. The protocol lists a set of methods, some of which must be implemented, and some are choice implementations (@optional). Compliance with the protocol is a way to implement all the protocols that must be implemented.  

 

1. Define an agreement

     Preparation of protocol format
@protocol protocol name//a meaningful name for the protocol //Method declaration list
@end

2
. Certain classes comply with some agreements
@interface class Name: Parent class < protocol name 1, protocol name 2,...> //... code ...
@end

keyword for method declaration
There are 2 keywords in the protocol that control whether the method is to be implemented (by default, @required), and in most cases the use of communication between programmers
@required: This method must be implemented (if not implemented, the compiler warns)
@optional: This method is not necessarily implemented


Agreement Compliance Agreement1. One protocol can comply with many other protocols, with multiple protocols separated by commas,
2. An agreement that complies with other agreements is equivalent to having a method statement in another agreement
@protocol Agreement name < Protocol 1, protocol 2>
@end

Base Protocol  NSObject is a base class, the most fundamental and basic class, and any other class will eventually inherit it.
There is also a protocol, named NSObject, which is a base protocol, the most fundamental and basic protocol
Many of the most basic methods are stated in the NSObject protocol, such as description, retain, release, etc.
It is recommended that each new agreement be subject to the NSObject agreement.the base class is compliant with the base protocol.   attributes declared in the @property can also be used as a limitation of compliance with the agreement  @property (Nonatomic,strong) class name < protocol name > * Type pointer location of the agreement  the protocol can be written directly in the class, written before @interface, so that it can be used directly and not in the #import, when only this class use this protocol, you can write the protocol inside.    Of course, it can also be defined in an external. h File   Proxy mode of the Protocol  @property (Nonatomic,strong) class name < protocol name > * Type pointer, you can see that you want to pass in the data then this data must be consistent with this Protocol, otherwise there will be an error, so you can relax the type of incoming data, As long as the conditions are met can be passed in. Here's one I wrote.A small example of proxy modemain.m
1 //2 //main.m3 //Protocal Ultimate Exercise4 //5 //Created by Keeganlee on 15/2/11.6 //Copyright (c) 2015 Keeganlee. All rights reserved.7 //Ten  One #import<Foundation/Foundation.h> A #import "Student.h" - #import "Agenta.h" - #import "Agentb.h" the #import "MyprotocalB.h" - intMainintargcConst Char*argv[]) { - @autoreleasepool { -        +         ID<MyprotocalA> Agenta=[[AgentA alloc] init]; -          +         ID<MyprotocalB> agentb=[[Agentb alloc] init]; A          at          -Student *stu=[studentNew]; -          -Stu.d=AgentA; -          - [Stu Buyticket]; in          -Stu.d=Agentb; to          + [Stu Buyticket]; -            the          *          $     }Panax Notoginseng     return 0; -}

Person H

////Person.h//Protocal Ultimate Exercise////Created by Keeganlee on 15/2/11.//Copyright (c) 2015 Keeganlee. All rights reserved.//#import<Foundation/Foundation.h>#import "MyprotocalB.h"@interfacePerson:nsobject <MyprotocalB>@property (nonatomic,assign)intAge ;@end

Person.m

// //   person.m//  protocal Ultimate Exercise ////  Created by Keeganlee On 15/2/11. //   Copyright (c) 2015 Keeganlee. All rights reserved. // #import " Person.h " @implementation  Person @end

Student.h

#import " Person.h " @interface  ID<MyprotocalB,MyprotocalA> D; // because it is an ID type, in the implementation of the student, it is certainly not possible to invoke the unique method with the object of the class that follows this protocol ~ ~ But this time with this protocol, it is tantamount to telling the compiler that these methods can be used. So the compiler is allowed to pass!! ~-(void) buyticket; @end

Student.m

#import " Student.h " @implementation Student-(void) buyticket{    int c=[_d Ticketremain];  there are no agent two classes here. H file But the compiler can still recognize this specific method which is the benefit of the interface!!     NSLog (@ "there has%d ticket left~", c);    [_d confirmtobuy];} @end

Agenta.h

#import <Foundation/Foundation.h>#import"MyprotocalB.h"@ Interface agenta:nsobject <MyprotocalB>@end

Agentb.h

#import <Foundation/Foundation.h>#import"MyprotocalB.h" @interface Agentb:nsobject <MyprotocalB>@end

agenta.m

#import " Agenta.h " @implementation AgentA-(int) ticketremain{    return;} -(void) confirmtobuy{    NSLog (@ "ok~~!! you have got the ticket~~  ");} @end

Agentb.m

#import " Agentb.h " @implementation agentb-(int) ticketremain{    return;} -(void) confirmtobuy{    NSLog (@ "SORRY ~all Ticekt has been solded~~~~~ ");} @end

Myprotocala

#import <Foundation/Foundation.h>@protocol myprotocala <nsobject>-(int  ) Ticketremain; -(void) confirmtobuy; @end

Myprotocalb

#import <Foundation/Foundation.h>#import"MyprotocalA.h" @protocol Myprotocalb <NSObject,MyprotocalA>@end

The above example is to achieve a ticket to buy the agent model, which incorporates the inheritance, polymorphism, etc. learned a lot of knowledge before. (Annotated in a more obscure place)

Dark Horse Programmer--oc Foundation eighth 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.