OC-Class category, extension, agreement

Source: Internet
Author: User

Category

Divided into two parts:
1. The system has written a class: According to the function of the system's class method to distinguish.
The class begins with @interface, followed by the current class name, the class name is the function of the classification, and ends with the @end.
2. The classes we create are generally designed to extend the class of the system.
Add files to the system's Class (example: NSString) objective-c file {file:stringmethod; File type:category; Class:nsstring}, @interface NSString (Stringmethod) appears automatically. The file name is: Nsstring+stringmethod.

@interface NSString (StringMethod)@end
Extend

Add a class to the class you wrote.
The extension begins with @interface, then writes the currently extended class name, adds a () to the class name, and ends with @end.
The general extension will be written in the. m file of the class you write, putting some properties that you do not want the external call in the extension, so that this property can only be used inside the class and not externally, to the maximum extent possible to secure the current class.
The categories are generally invisible. M's system files are extended, and the extension is generally done for their own classes.
The. m file for the class:

// 这其中的name属性和create方法只能在自己类中使用.@interface Student ()@property(nonatomic, copy)NSString *name;@end@implementation Student- (void)create{    _name = @"你好";    NSLog(@"%@",_name);}@end
Agreement
    1. Declare the protocol @protocol in the. m of the class, the protocol name is the class name +delegate, and then write the contents of the Protocol (@required) and choose to Implement (@optional));
 #import  <foundation/foundation.h>   //1. Declaration of an agreement  //@protocol protocol keyword   @protocol  marry  << Span class= "Hljs-title" >nsobject ;  //protocol content:  //protocol method declaration: divided into two kinds of  //if not indicated @ .... Is the method that must be implemented.  //@optional is selectable   @required -(void ) Earnmoney; @optional -(void ) Cook;  @end   
    1. Set agent properties:
      Write in @interface ...: nsobject
      @property (nonatomic, assign) ID < protocol name >delegate;
      Write Trigger method.
@interface Girl : NSObject// 2.设置代理人的属性@property(nonatomic,assign)id<Marry>delegate;// 触发方法- (void)getMarry;@end
    1. Write what the agent needs to do in the. m of the class.
#import "Girl.h"@implementation Girl//两个人结婚之后,协议才正式的生效.- (void)getMarry{    //3. 让代理人去执行协议里的方法.    [self.delegate earnMoney];}@end
    1. In the agent. h the first header file, and then signed the agreement. nsobject< Agreement name >;
#import <Foundation/Foundation.h>#import "Girl.h"//4.引完头文件之后,boy需要签订协议@interface Boy : NSObject<Marry>@end
    1. Identify the agent in the MAIN.M
#import <Foundation/Foundation.h>#import "Boy.h"#import "Girl.h"int main(intconstchar * argv[]) {    // 创建两个小人    Boy *boy = [[Boy alloc] init];    Girl *girl = [[Girl alloc] init];    // 5.设置代理人    girl.delegate = boy;    //7. 通过调用结婚的方法,来执行协议里的方法.    [girl getMarry];    return0;}
    1. The method content that needs to be implemented in the agent. M write protocol.
#import "Boy.h"@implementation Boy//6.实现协议方法earnMoney- (void)earnMoney{    NSLog(@"买买买");}@end

Trigger.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OC-Class category, extension, agreement

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.