(object-c) study notes (iv)--OC protocol, classification, extension

Source: Internet
Author: User

OC's Protocol

  A protocol is a rule, and defining a protocol is tantamount to making a rule. Classes in OC can obey rules, and classes that comply with the Protocol are equivalent to one or more capabilities. Some properties and methods are put into one protocol, which can be easily added to other classes.

Definition of the protocol:

@protocal

@required declares the properties and methods that must be followed by default

@optional declaration of optional properties and methods of compliance

@end

Compliance with the Agreement:

@interface trmyclass: nsobject<trmyprotocol> The agreement follows multiple protocols in angle brackets @interface trclassc: nsobject<TRProtocol4, TRProtocol5>

Create a protocol:

Select Object-c File to enter.

Type selection protocol.

The protocol is an. h file:

1 #import <Foundation/Foundation.h>23@protocol trmyprotocol <nsobject >4 @property NSString *name; 5 -(void) show; 6 @end

Classes that comply with the agreement

TRMyClass.h

1 #import <Foundation/Foundation.h>2#import"TRMyProtocol.h  "34@interface trmyclass:nsobject<trmyprotocol>5 @property NSString *zsname; 6 -(void) fun; 7 @end

Trmyclass.m

1 #import "TRMyClass.h"2 3 @implementationTrmyclass4 @synthesizeName =_name;5 6-(void) Show7 {8NSLog (@"1 name is%@", self.name);9NSLog (@"2%@", self.zsname);TenSelf.zsname =@"John Doe"; OneNSLog (@"3%@", self.zsname); ANSLog (@"4%@", _name); -      - } the  --(void) Fun - { -NSLog (@"non-Protocol method"); + } -  + @end

Main.m

1  #import<Foundation/Foundation.h>2  #import "TRMyProtocol.h"3 #import "TRMyClass.h"4  5  voidTest ()6 {7      ID<TRMyProtocol>ref=[[Trmyclass alloc] init];8       ref. Name =@"Zhang San";9[refshow];Ten      //[ref fun]; One  } A intMainintargcConst Char*argv[]) { -  - @autoreleasepool { the  -          -  - test (); +  -          +  A     } at  -     return 0; -  -}

Categories of OC

  What are categories:

    Classification is the complement and extension part of a class

Classification is part of a class

Classify to add specific capabilities to a class

To create a category:

Select Category

The following class becomes optional

TRFraction.h

1 #import <Foundation/Foundation.h>23@interface  trfraction:nsobject  4int  n; 5 int D; 6 -(void) fun; 7 -(void) show; 8 @end

Trfraction.m

1 #import "TRFraction.h"2 #import "nsobject+trshow.h"3 4 @implementationtrfraction5-(void) Fun6 {7NSLog (@"In main class fun");8 }9-(void) ShowTen { One [Super show]; ANSLog (@"%d/%d", SELF.N, SELF.D); - } - @end

TRTest.h

1 #import " TRFraction.h " 2 3 @interface trfraction (trtest) 4 -(void) showtest; 5 @end

You can also rewrite the Show method here, I rewrite the show method because of other classifications, and if you write the Show method again in this category there is a warning.

Trtest.m

 1   #import   " trfraction+trtest.h   " 2  @ Implementation   Trfraction (trtest)  4 -(void  ) showtest  5  6  NSLog (@ "  show test  "  ); 7   8   @end  

MAIN.M inside the import can use the method of classification, the method of classification is equivalent to the method of the class, no difference.

Http://www.cnblogs.com/wendingding/p/3709569.html

Expansion of OC (extended)

  Extensions are special classifications, and extensions are categories with no names. Extension is generally defined in the. m file, the extension can define attributes, methods, strength variables, all of which are private and can only be used in. m files.

The difference between classification and extension

  Classification: Classification cannot define variables, usually public, category file name "Main Class class Name + Class name"

Extension: extension is usually private, you can define an instance variable, extend the filename "main class class name _ Extension class name"

TRFraction.h

1 #import <Foundation/Foundation.h>23@interface  trfraction:nsobject  4int  n; 5 int D; 6 -(void) show; 7 @end

Trfraction.m

1 #import "TRFraction.h"2 #import "Trfraction_myextension.h"3 4 @interfaceTrfraction ()//The nameless category is the extension5@propertyintValue//of Private6-(void) ExtensionMethod1;//of Private7 @end8 9 @implementationtrfractionTen-(void) Show One { ANSLog (@"%d/%d", SELF.N, SELF.D); - } --(void) EXTENSIONMETHOD0 the { -NSLog (@"extension Methods"); - } --(void) ExtensionMethod1 + { -NSLog (@"extension Method 1"); + } A @end

  

(object-c) study notes (iv)--OC protocol, classification, extension

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.