고유 문법,분류Category,protocol 프로토콜,block
.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;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.