Objective C for iOS training courses-classification and Protocol

Source: Internet
Author: User

If everyone else is running and you are still leaving, you have to start to reflect on what is going on? The brain is not inferior to others, and the hands are not slower than others. In fact, you have not chosen the right industry, and you have chosen the right industry to surpass yourself.IOS TrainingSeize high-paying employment opportunities.

Classification and protocols

I. Classification

1. Classification: classification provides a simple way to modularize the definition of a class to a related method group or classification. It also provides a simple way to extend the existing class definition, and does not need the source code of the helper class.

2. usage, for example, adding a new category for the Fraction class

# Import "Fraction. h"

@ InterfaceFraction (Mathops)

-(Fraction *) add :( Fraction *) f;

-(Fraction *) mul :( Fraction *) f;

-(Fraction *) sub :( Fraction *) f;

-(Fraction *) div :( Fraction *) f;

@ End

Note: It is both an interface definition and an extension of the existing interface. Therefore, the original interface must be included, so that the compiler knows the Fraction class unless the new category is directly combined with the header file of the original Fraction.

3. Notes on Classification

1> although the category can access the instance variables of the original class, it cannot add any of its own variables.

2> classification can be used to overload another method in the class. However, this method is often considered to be a poor design habit.

3> there are many categories, as long as they comply with the rules described here.

4> unlike the general interface, you do not have to implement all the methods in classification.

5> extend a class by adding a new method to the category will not only affect this class, but also affect all its subclasses.

6> adding new methods to existing classes by category may be useful to you, but they may be inconsistent with the original design or intent of the class.

7> the object/category name pair must be unique. However, in a given Objective-C namespace, only one NSString (private) category can exist.

Ii. Agreement

1. Protocol: A list of methods shared by multiple classes. The methods listed in the Protocol are not implemented by others. The Protocol provides a way to use a specified name to define a set of somewhat relevant methods.

2. Definition method: Use the @ protocol command followed by the protocol name.

For example, define the NSCopying protocol in the standard header file NSObject. h.

@ ProtocolNSCopying

-(Id) copyWithZone :( NSZone *) zone;

@ End;

3. Adopt Protocol: If your class adopts a protocol, you must implement some methods in the Protocol.

For example, to use the NSCopying protocol, you must implement the copyWithZone method.

@ InterfaceAddressBook: NSObject (NSCopying>.

Note: To use multiple protocols, you only need to place them in angle brackets and separate them with commas.

4. Check whether an object complies with a certain protocol.

For example, a currentObject object is called to check whether it complies with the Drawing protocol.

IdcurrentObject;

......

If ([currentObjectconformsToProtool: @ protocol :( Drawing)] = YES)

{

// SendcurrentObjectpaint, eraseand/oroutlinemsgs

......

}

Here, the dedicated @ protocol command is used to obtain a protocol name and generate a protocol object. conformsToProtool: The method expects this object as its parameter.

5. informal protocols)

1> concept: it is actually a classification, listing a group of methods but not implementing them.

2> Add method: Use @ optional.

3> adoption of informal protocols: all methods of this Protocol are not required, depending on this protocol.

6. Note

1> the Protocol does not reference any class. It is non-class.

2> like the class name, the protocol name must be unique.

3. Merging objects

1. Concept: Define a technology that contains one or more objects of other classes.

2. merging method, for example, defining a Square class with a Rectangle class

@ InterfaceSquare: NSObject

{

Rectangle * rect;

}

-(Int) setSide :( int) s;

-(Int) side;

-(Int) area;

-(Int) perimeter

@ End

3. A solution is to overload init or add new methods such as initWithSide to allocate space.

Classification and protocols are briefly introduced here. If you can understand and understand clearly, and don't want to be left behind by others, you will not be able to take the initiative to keep up with the pace of society and learn together.IOS TrainingCourse, learning iOS development technology.

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.