고유 문법,분류Category,protocol 프로토콜,block

4078 단어
Category는 원래 클래스 코드를 바꾸지 않는 기초 위에서 클래스를 확장하는 방법
.h
#import "Student.h"
//  ()        
// ()  Tests      
@interface Student (Test)
//         ,        
	-(void)test;
@end

@implementation Student (Test)
//        	
-(void)test{
	NSLog(@"test  ");
	}
@end

main.m--파일
#import <stdio.h>
#import "Student.h"
#import "Student+test.h"
int main(int argc, const char * argv[])
{
 	Student *stu=[[Student alloc]init];
	[stu test]; //           
    return 0;
}

protocol 프로토콜
버튼 모니터를 예로 들면 다음과 같습니다.
Buttom.h
#import <Foundation/Foundation.h>
@class Button;
//                   ,          。
//            ,       ,        ,        
// <>        
@protocol ButtonDelegate<NSObject>
-(void) onClick:(Button *)but;
@end 
@interface Button:NSObject
// delegate        
@property (nonatomic,retain) id<ButtonDelegate> delegate;
//     
-(void)click;
@end

Button.m
#import "Button.h"
@implementation Buttom
-(void) dealloc{
	[_delegate release];
	[super dealloc];
}
@end

//프로토콜의 클래스를 사용합니다.
ButtonListener.h
#import <Foundation/Foundation.h>
//          , @class       
@protocol ButtonDelegate;
@interface ButtonListener : NSObject<ButtonDelegate>
-(void)onClick:(Button *)but;
@end


//            
ButtonListener.m
#import "ButtonListener.h"
@implementation ButtonListener
-(void)onClick:(Button *)but{
    NSLog(@"  %@    ",but);
}
@end

//감청기 프로토콜을 실현하는 또 다른 종류
MyListener.h
#import <Foundation/Foundation.h>
#import "Button.h"
@interface MyListener : NSObject<ButtonDelegate>
-(void)onClick:(Button *)but;
@end

MyListener.m
#import "MyListener.h"
@implementation MyListener
-(void)onClick:(Button *)but{
    NSLog(@"  MyListener    ");
}
@end

main.c
#import <Foundation/Foundation.h>
#import "Button.h"
#import "ButtonListener.h"
#import "MyListener.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //        
        Button *but=[[Button alloc] init];
        
        //           ,    but
        but.delegate=[[ButtonListener alloc] init];
        
        //       
        [but click];

        
        //         
        but.delegate=[[MyListener alloc] init];
        
        //       
        [but click];
    }
    return 0;
}

block
//blcok를 정의했습니다. 이 Block은 int 형식을 되돌려주고 두 int 형식의 매개 변수를 받아들입니다.
 int (^Sum)(int,int)=^(int a,int b){
        return a + b;
    };
    int a = Sum(10,10);
    NSLog(@"%i",a);

    //   typedef   
    typedef int(^MySum) (int,int);
    //      blck  
    MySum sum=^(int a,int b){
        return a + b;
    };

실제 응용프로그램: Button 감청기
예:
Button.h
#import <Foundation/Foundation.h>
@class Button;

// typedef int(^MySum) (int,int);
typedef void (^ButtonBlock) (Button *but);

@interface Button : NSObject
// block   * ,        block   
@property (nonatomic,assign) ButtonBlock block;
-(void)click;
@end

Button.m
#import "Button.h"
@implementation Button
-(void)click{
    _block(self);
}
@end

main.m
#import <Foundation/Foundation.h>
#import "Button.h"
int main(int argc, const char * argv[])
{ 
    @autoreleasepool {
        
        Button *but=[[Button alloc] init];
	//     block   but block
        but.block=^(Button *but){
            NSLog(@"  %@      。",but);
        };
        //       
	//
        [but click];  
  }
    return 0;
}

//Block은 액세스할 수 있으며 외부는 변수가 아니지만 외부 변수는 수정할 수 없음
   int c=10;
//외부 변수가 을 사용한 경우Block 키워드, 수정 가능
   __block int d=10;

좋은 웹페이지 즐겨찾기