Objective-C Protocols, objective-c

Source: Internet
Author: User

Objective-C Protocols, objective-c

Objective-C Protocols

1.1 Formal Protocols

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

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

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

Formal protocol requires you to explicitly adopt it.

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

You use this protocol by listing the name of the protocol in your class @ interface.

When you do this, your class is said to conform to the protocol

When you adopt the protocol, you follow it.

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

When you use this protocol, you promise to implement all the protocol methods. If you do not use it, the compiler generates a warning.

 

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

Formal protocols is similar to java interfaces.

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.

Syntactically, it is similar to declaring a class or category syntax.

You can also can have 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 a parent protocols as if you have a parent classes. To declare a parent protocol, you must declare the parent class in a angle bracket and behind a protocol.

@ Protocol MySuperDuberProtocol <MyParentProtocol>

@ End

The preceding lines mean that MySuperDuperProtocol extends MyParentProtocol, so you have to satisfy the method implementation of all the required methods in both protocols.

You must implement all the methods required in these two protocols.

 

@ Protocol NSCoding

-(Void) encodeWithCoder: (NSCoder *) encoder;

-(Id) initWithCoder: (NSCoder *) decoder;

@ End

1.3 Adopting a Protocol uses a protocol

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

When you want to use a protocol, you put the protocol 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 re sending a message to programmers reading the class declaration, saying that objects of this class can do two very important things: they can encode/decode themselves and copy themselves.

When protocol is used, you can send another message to a programmer: This object can implement two very important things.

 

1.4 Copies

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 caller.

A copy method backs up an object. This copy information will tell an object to create a series of new objects, and make the new object the same as the object of the message recipient.

Making Copies

Most objects refer to-that is, point at-other objects.

Most objects references, that is, to 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 make a copy of an NSArray, your copy only duplicates the pointers to the referred objects, not the objects themselves. if you copy an NSArray that holds five NSStrings, you still end up with five strings running around your program, not ten. in that case, each object ends up with a pointer to each string.

When you want to create a copy, you do not copy the object to which it is directed. Your new copy only points to the existing object to be directed.

A deep copy, on the other hand, makes duplicates of all the referred objects. if NSArray's copy was a deep copy, you 'd have 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 adopt the NSCopying protocol, we must implement the copyWithZone method.

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

A zone is an NSZone where you can allocate memory. When you send a copy message to an object, it turns to a copyWithZone method before it comes into contact with 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 [self class], the allocWithZone: will be sent to the class of the object that is processing the copy message.

If [self class] is used, allocWithZone will be passed to the class of this object and accept the 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 have to make sure AllWeatherRadial's rain-and snow-handling instance variables are 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 you 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 specifies a pointer that can point to any class. It makes the general object type.

You can assign any object to an id variable, and you can assign an id variable to any kind of object pointer.

You can assign any object to an id variable. You can assign any id variable to any object pointer.

If you follow id with a protocol name, complete with angle brackets, you're telling the compiler (and any humans reading the code) that you are expecting any kind of object, as long as it conforms to that protocol.

If you add an id before a protocol name, 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 two new modifiers for protocols: @ optional and @ required.

The Objective-C2.0 added two new changes 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 Will Come to Order

Delegation is a design pattern that allows an object to designate another object to handle a special task.

Delegation is a design mode that allows an object to specify another object to execute 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 set, or nil.

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

Method 2: Set delegate. The parameter type of the delegate tells us that we can set any objects that follow the predefined 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.