From C # To Object C, gradually learn about Apple Development (3) -- Classification and Protocal

Source: Internet
Author: User

1. classification concepts and usage if we have used C #, we all know that C # contains something called an extension function, which can be inherited without inheriting existing classes, add some interface functions that are not originally available to existing classes. The classification concept of Object C is similar to that of Object C. It can even be said to be the same type. Although they do not know who is first and who will appear, the introduction of this item makes programming richer and more efficient. Objective-C provides a distinctive way-Category, which can dynamically add new behaviors for existing classes. In this way, the original design scale of the class is small, and the functions are gradually expanded when they are added. When you use Category to expand a class, you do not need to access its source code or create a subclass. Category uses a simple method to modularize the class-related methods and allocate different class methods to different classification files. However, Category cannot extend attributes for the class, because Object C does not support such attribute extension. The definition Syntax of Category is as follows. @ Interface ClassName (CategoryName) @ end it seems that they have a common habit to name declared files and implemented files in the form of "original class name + Category. So although OC functions are similar to C #, this Convention is different from C #. C # Can be used wherever you put it, but we should respect its rules. For example, we add an extension method to the XYZPerson class as follows. The Defined Function convention is put in the file "XYZPerson + XYZPersonNameDisplayAdditions. h. # Import "XYZPerson. h "@ interface XYZPerson (XYZPersonNameDisplayAdditions)-(NSString *) testMethod; @ end, its implementation code is as follows. Its code convention is put into" XYZPerson + XYZPersonNameDisplayAdditions. m. Copy the Code # import "XYZPerson + XYZPersonNameDisplayAdditions. h "@ implementation XYZPerson (XYZPersonNameDisplayAdditions)-(NSString *) testMethod {return [NSString stringWithFormat: @" % @, % @ ", self. lastName, self. firstName] ;}@ end copy the code in C #. The extension method is related to the namespace. Once the namespace range is exceeded, this extension function does not work, object C, like a class, does not have a namespace concept. Therefore, you need to be careful when you expand it. Otherwise, the classification interface may conflict with the class itself. For this reason, we recommend that you add a prefix to the classification interface and use the consistent interface rules for naming, as shown in the following code. @ Interface NSSortDescriptor (XYZAdditions) + (id) xyz_sortDescriptorWithKey :( NSString *) key ascending :( BOOL) ascending; although the extension method name like @ end is longer, however, it is basically guaranteed that it does not conflict with common interface methods. Use Cases of Category: 1. When you define a class, you may want to add methods to one or more classes in some situations (such as demand changes. 2. A class contains many different methods that need to be implemented, and these methods need to be implemented by members of different teams. 3. When you are using a class in the basic class library, you may want these classes to implement the methods you need. When you encounter these requirements, Category can help you solve the problem. Of course, there are also some problems with Category. 1. Category can access the instance variables of the original class, but cannot add variables. If you want to add variables, you can consider creating subclasses by inheriting them. 2. Category can reload the methods of the original class, but it is not recommended to do so. The consequence of doing so is that you can no longer access the original method. If you do need to reload, the correct choice is to create a subclass. 3. What is different from a common interface is that you do not need to implement all declared methods in the classification implementation file, as long as you do not call it. There is also a function of class extension, which is applicable to classes with code, that is, when your class code and your extended source code are compiled at the same time. The class extension method is similar to the class above. They do not need to write the name of the extended class. This is a bit like the concept of anonymous extended classification, as shown below @ interface ClassName () @ end: this anonymous extended Category is different from the common Category. In addition to methods, it can also add attributes or variables. 2. the Protocal concept is similar to the C # interface, but it is different. It can be an optional implementation interface @ optional or a required implementation interface @ required, although Object C already has a keyword @ interface, this is different from Protocal. Like the C # interface, this protocol can also inherit from another Protocal, that is, they can have an inheritance relationship. @ Protocol NewProtocal <Protocal> @ end many applications developed by Object C, such as IOS applications, all use the proxy mode in the MVC development model, this Protocal is very good at handling this relationship. In iOS and OS X development, Apple adopts a large number of proxy modes to decouple View and Controller in MVC. For example, all events generated by UIView are delegated to the Controller. According to the conventions, all objects suffixed with Delegate are Protocol, such as UIApplicationDelegate and UIWebViewDelegate. In C #, there are many interfaces, such as IClonable and IEnumerable, which can be cloned and enumerated as long as they are implemented. In the Object, Protocal can be used instead, if a protocol inherits NSObject, this indicates the Protocol declared here. It is a derivative protocol of the NSObject protocol (not the NSObject class). That is to say, the context here understands that NSObject is a protocol, if it is the inheritance relationship in @ Interface, it is the NSObject object. A little interesting. The following is an optional and mandatory Protocol definition example. Copy the code @ protocol XYZPieChartViewDataSource-(NSUInteger) numberOfSegments;-(CGFloat) values :( NSUInteger) segmentIndex; @ optional-(NSString *) values :( NSUInteger) segmentIndex;-(BOOL) shouldExplodeSegmentAtIndex :( NSUInteger) segmentIndex; @ required-(UIColor *) colorForSegmentAtIndex :( NSUInteger) segmentIndex; @ end copy code is optional and required because of the Protocol, if we want to know whether a dynamic object has an interface function, it is determined by the @ selector operator. NSString * thisSegmentTitle; if ([self. dataSource respondsToSelector: @ selector (titleForSegmentAtIndex :)]) {thisSegmentTitle = [self. dataSource titleForSegmentAtIndex: index];} similar to the C # interface definition, a Class Object of Object C can implement multiple protocols, the following example shows how to implement several protocols for a class interface definition. @ Interface MyClass: NSObject <MyProtocol, AnotherProtocol, YetAnotherProtocol>... @ end: in this way, the MyClass object has only one base class object, but multiple protocols (C # is multiple interfaces) can be implemented.

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.