[Objective-C] interface & Protocol & categories

Source: Internet
Author: User
1 interface (interface conventions)

Interfaces are interpreted as interfaces. What is different from interfaces in Java \ C # Is that interfaces in obj-C should be considered as informal protocols, the interface in obj-C only indicates that the declared message (method) should be processed, but not required.

Indicates that the helloworld message should be processed in the sample interface, but the sample. m, we can not implement the helloworld processing, so the compiler will give a warning but the compilation can pass, but sending this message to the sample in the program will cause an exception.

2 protocol (formal protocol)

Protocol (formal protocol) looks more formal and more semantic: the class that requires the protocol, "must" implement the methods agreed in the Protocol. However, it is entertaining that, even if it is known as a formal protocol, the compiler still gives a warning when it encounters noncompliance during compilation. You will think that protocol is similar to interface, and protocol design is redundant. In fact, Protocol has the following important significance:

Protocol (Formal Agreement)The method definition in the business can be separated to form a separate file, which is similar to the traditional OOThe extraction interface in is consistent. If two systems need to exchange data, you can develop a set of protocols that both parties comply.And add the protocol file to the project in both systems to implement it.

This function cannot be implemented by informal protocols (@ interface. Because obj-C does not allow many inheritance, both systems may need to inherit a class to implement certain functions. At this time, only one protocol can be implemented ). In addition, the Protocol is extended in obj-C 2.0, allowing methods in the protocol to be identified as "required (@ required)" and "optional (@ optional, if the methods in the Protocol are identified as @ optional, the compiler will not give a warning even if the classes using the Protocol do not implement these methods. In this way, many functions of the interface conventions can be implemented by the Protocol, but the Protocol cannot contain data (but the interface conventions can contain data members ).

Nsobject is an interface and protocol, while interface nsobject implements protocol nsobject. In the OO world of obj-C, nsobject, as the ancestor of everything, is actually a "formal protocol ", therefore, all classes derived from nsobject only comply with one or more protocols.

3 categories (type classification, informal Protocol)

The informal protocol is not designed as a relaxed version of the formal protocol, but as a class categories ). Categories is one of the most commonly used functions in objective-C. In general, categories allows us to add methods to existing classes without adding a subclass. You do not need to know its internal implementation. This is very effective if we want to add methods of classes that come with a framework. If we want to add a method to nsstring in our program project, we can use categories without implementing a subclass of nsstring. For example, if we want to add a method in nsstring to determine whether it is a URL, we can do this:

1) In the categoriestest. h header file:

   1:  @interface NSString (URLDetecter)
   2:  - (BOOL) isURL;
   3:  @end

This is very similar to the class definition. The difference is that the category does not have a parent class, and the category name must be included in the brackets. The name can be retrieved at will, but it is a habit of calling a method that makes people better understand some functional methods in category. Here is the specific implementation. However, note that this is not a good way to judge the URL. We mainly aim to understand the concept of category as a whole.

2) provide the implementation in categoriestest. M.

   1:  #import "categoriesTest.h"
   2:  @implementation NSString (URLDetecter)
   3:  - (BOOL) isURL{  
   4:    if ( [self hasPrefix:@"http://"] )    
   5:        return YES;
   6:    else 
   7:       return NO;  
   8:  }@end

3) When nsstring is used in Main. m, it is as follows:

   1:  NSString* string1 = @"http://www.CocoaDev.cn/";
   2:  NSString* string2 = @"Pixar";
   3:  if ([string1 isURL])  NSLog(@"string1 is a URL");
   4:  if([string2 isURL])  NSLog(@"string2 is a URL");

4) if the main. M file does not have the import header file categoriestest. H, the following warning will be given:

Warning: 'nsstring' may not respond to '-isurl'

Warning: (messages without a matching method Signature

Warning: will be assumed to return 'id' and accept

Warning: '...' as arguments.

The compiler does not think that nsstring will respond to the isurl of the message.

The running result is as follows:

5) If you import the header file categoriestest. h In Main. m

The running result is as follows:

Unlike subclass, category cannot add member variables. We can also use category to override the existing methods of the class, but this requires great caution. Remember, when we modify a class through category, it takes effect for all objects of this class in the application.

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.