OBJECTIVE-C protocols

Source: Internet
Author: User

OBJECTIVE-C protocols?

1.1 Formal protocols?

A formal protocol (like the informal protocol) is a named list of methods and properties.

Formal protocol (like informal protocol) is a list of names for a series of methods and properties.

However, a formal protocol requires that you explicitly adopt it.

Formal protocol need you to adopt it explicitly.

You adopt a protocol by listing the protocol ' s name in your class ' s @interface declaration.

You use this protocol by listing Protocol's name in your class @interface.

Your class is said to conform to the protocol

When you adopt it, you are obeying the agreement.

Adopting a protocol means that's promise to implement all the methods of that protocol. If you don ' t, the compiler yells at your by generating a warning.

When you adopt this protocol means that you promise to implement all the Protocol methods. If you don't, the compiler will create a warning for this.

?

Formal protocols is just like Java interfaces. In fact, OBJECTIVE-C protocols were the inspiration for Java ' s interfaces.

Formal protocols is much like Java's interface

1.2 Declaring protocols declaration protocols?

?

If you adopt nscopying, your object knows how to make copies of itself:

@protocol nscopying

-(ID) Copywithzone: (Nszone *) zone;

@end

The syntax looks kind of the same as the syntax for declaring a class or a category.

The syntax of a clause is much like declaring a class or category.

You can also can has parent protocols, similar to parent classes. To specify the parent protocol, declare it in angle brackets after the name of the protocol.

You can have the parent protocols as you have the parent classes. In order to declare a parent protocol, you must declare the parental class inside an angle bracket, behind a protocol.

@protocol Mysuperduberprotocol <MyParentProtocol>

@end

The preceding lines mean that Mysuperduperprotocol extends Myparentprotocol and so you had to satisfy the method Implementat Ion of the required methods in both protocols.

You have to implement all the methods required in both protocols.

?

@protocol nscoding

-(void) Encodewithcoder: (Nscoder *) encoder;

-(ID) Initwithcoder: (Nscoder *) decoder;

@end

1.3 Adopting a Protocol use a Protocol?

To adopt a protocol, your list the protocol in the class declaration, surrounded by angle brackets.

When you want to use a protocol, you put the agreement in the class declaration, surrounded by angle brackets.

@interface Car:nsobject <NSCopying>

{

Instance variables

}

Methods

@end//Car

And if Car adopts both nscopying and nscoding, the declaration goes like this:

@interface Car:nsobject <nscopying, nscoding>

{

Instance variables

}

Methods

@end//Car

When you adopt a protocol, you ' re sending a message to programmers reading the class declaration, saying that objects of T His class can do the very important things:they can encode/decode themselves and copy themselves.

When you use protocol, you send a message to a programmer: This object can accomplish two very important things.

?

1.4Copies?

The copy method, of course, makes a copy of an object. The copy message tells an object to create a brand new object and to make the new object the same as the receiver.

When a copy method is used, a backup is made for an object. This copy information tells an object to create a series of new objects, and makes the new object the same as the object of the message recipient.

Making Copies?

Most objects refer To-that are, point at-other objects.

Most objects references, that is, point to an object.

When you create a shallow copy, you don ' t duplicate the referred objects; Your new copy simply points at the referred objects that already exist. Nsarray ' s Copy method makes shallow copies. When you do a copy of an nsarray, your copy is only duplicates the pointers to the referred objects, not the objects Themse Lves. If you copy a nsarray that holds five nsstrings, you still end to with five strings running around your program, not ten. In this case, each object ends up with a pointer to each string.

When you want to create a shallow copy, you do not copy the object that is pointed to. Your new copy simply points to an already-existing object that is pointed to.

A deep Copy, on the other hand, makes duplicates of the referred objects. If Nsarray ' s copy is a deep copy, you ' d had ten strings floating around after the copy was made.

?

@interface Engine:nsobject <NSCopying>

@end//Engine

Because we ' ve adopted the Nscopying protocol, we have to implement the Copywithzone:method.

Because we have adopted the Nscopying protocol, we must implement the Copywithzone method.

A zone is a nszone, which are a region of the memory from which you can allocate memory. When you send a copy of message to an object, it gets turned into copywithzone:before reaching your code.

A zone is a nszone, and he is the area where you can allocate memory. When you send a copy message to an object, it turns to a Copywithzone method before it touches your code.

Here ' s Engine ' s copywithzone:

Implementation:

-(ID) Copywithzone: (Nszone *) zone

{

? ? Engine *enginecopy;

? ? Enginecopy = [[Self class]

? ? Allocwithzone:zone] init];

? ? return (enginecopy);

}//Copywithzone

By using the "Self Class", the Allocwithzone:will be sent to the class of the object, which is receiving the copy message.

If you use [self class], Allocwithzone will be passed the object's class, accepting copy information.

@interface Allweatherradial:tire

.... properties

... methods

@end//Allweatherradial

When Allweatherradial inherits from Tire, it pulls along all of Tire ' s baggage, including the conformance to the Nscopying Protocol.

We ' ll need to implement Copywithzone:though, because we had to make sure Allweatherradial ' s Rain-and snow-handling Inst ance variables is copied:

-(ID) Copywithzone: (Nszone *) zone

{

? Allweatherradial *tirecopy;

? tirecopy = [Super Copywithzone:zone];

? tirecopy.rainhandling = rainhandling;

? tirecopy.snowhandling = snowhandling;

? return (Tirecopy);

}//Copywithzone

1.5 Protocols and Data Types

You can specify protocol names in the data types your use for instance variables and method arguments.

You can specify the protocol name as the strength variable and method parameter in your data type

?

Recall that the ID type represents a pointer to any kind of object; It ' s the generic object type.

An ID type indicates a pointer that can point to any class. It makes a generic object type.

You can assign any object to the ID variable, and you can assign a ID variable to any kind of object pointer.

You can assign arbitrary objects to an ID variable, and you can assign any ID variable to any object pointer.

IF you follow ID with a protocol name, complete with angle brackets, your ' re telling the compiler (and any humans reading T He code) that is expecting any kind of the object, as long as it conforms to that protocol.

If a protocol name is preceded by an ID, you are telling the compiler that you can have any object.

-(void) Setobjectvalue: (id<nscopying>) object;

1.6 New features

. OBJECTIVE-C 2.0 added the new modifiers for protocols: @optional and @required.

Objective-c2.0 added two new modifications to protocol: @optinoal and @required?

@protocol Baseballplayer

-(void) drawhugesalary;

@optional

-(void) slidehome;

-(void) Catchball;

-(void) throwball;

@required

-(void) Swingbat;

@end//Baseballplayer

1.7 The delegation would Come to Order

Delegation is a design pattern, allows an object to designate another object to handle a particular task.

Delegation is a design pattern that allows an object to specify another object to perform special tasks.

?

-(ID <NSNetServiceBrowserDelegate>) delegate;

-(void) Setdelegate: (id <NSNetServiceBrowserDelegate>) delegate;

The first method returns the current delegate if it is set, or nil otherwise.

The first method returns the existing delegate if it is already set, or returns nil.

He second one sets the delegate. The type of the argument delegate tells us we can set any object as a delegate as long as it conforms to the expected Protocol.

The second method sets the delegate. The parameter type of the delegate tells us to be able to set arbitrary objects that conform to the predetermined protocol.

OBJECTIVE-C protocols

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.