您的位置:首页 > 移动开发 > Objective-C

Objective-C的协议、Blocks、Category

2015-04-29 10:31 537 查看

协议

协议定义

@protocol MyProtocol1<NSObject>

@required

-(void) showInfo;

@end

@protocol MyProtocol2<NSObject>

@optional

-(int) update;

@end

协议使用

@interface MyTest:NSObject< MyProtocol1,MyProtocol2>

-(void) showInfo2;

@end

协议实现

@implementation MyTest

-(void) showInfo

{

         NSLog(@”show  one”);

}

-(void) showInfo2

{

         NSLog(@“show  two”);

}

主函数

int main(int  argc,const  char *  argv[])

{

         @auotreleasepool{

                   id<MyProtocol1>  test=[[MyTest  alloc] init];//协议方式

                   //MyTest  *test=[[MyTest  alloc] init];

                   SEL  sel=@selector(showInfo:);

                   if  ([test respondsToSelector:sel )]){

                            [test showInfo];

                   }

                   [test  showInfo2];

                   [test  release];

}

return  0;

}

 Blocks

 

C函数指针

Blocks

声明

int   (*  CFunc)(int  a)

int   (^  BFunc)(int  a)

调用

int   ret=CFunc(100)

int   ret=BFunc(100)

typedef

typedef   int  (*SumT)(int  a,int   b)

typedef   int  (^SumT)(int  a,int   b)

 

 

__block关键字是变量全局

 

typedef int  (^SumT)(int  a,int b);

int main(int  argc,const  char *  argv[])

{

         @auotreleasepool{

                    __block int  sum=0;

                   int  (^myBlocks)(int  a,int b)=^(int  a,int  b){

                            sum=a+b;

                            return  1;

};
int  ret= myBlocks(10,20);
NSLog(@”sum  is  %d ret  is  %d”,sum,ret); 
SumT myBlocks2=^(int a,int  b){
         NSLog(@”ret  is  %d”,a+b);
         return  1;
};
myBlocks2(20,30);

}

return  0;

}

 Category

Category实现继承之外的扩展方法的机制,把一个类的实现分为若干不同文件中。

把声明和实现放到一个.m文件中,那么外界不能访问,实现类似private功能。

只能扩展函数,不能扩展字段,变量。一般命名规范为扩展类名+扩展变量.[hm]

实例
4000

声明

NSString+Reverse.h 

 

#import  <Foundation/Foundation.h>

@interface  NSString(reverse)

-(NSString *)  reverseString;

@end

实现

NSString+Reverse.m

 

#import  <Foundation/Foundation.h>

#import NSString+Reverse.h

@implementation  NSString(reverse)

-(id) reverseString{

         NSUInteger len=[self  length];

         NSMutableString  *retStr=[NSMutableString  stringWithCapacity:len];

         while  (len>0){

                   unichar  c=[self characterAtIndex:--len];

                   NSString *str=[NSString  stringWithFormat:@”%C”,c];

                   [retStr  appendString:str];

}

return  retStr;

}

@end 

主函数

#import <Foundation/Foundation.h>

#import NSString+Reverse.h

int main(int  argc,const  char *  argv[])

{

         @auotreleasepool{

                   NSString  *str=[NSString  stringWithString:@”Hello,world!”];

                   NSLog(@”String:%@”,str);

                   NSString  *ret=[str reverseString];

                   NSLog(@”Reversed:%@”,ret);

}

return  0;



私有化实例

#import “Foo.h”

@interface Foo(Private)

-(void) test2;

@end

 

@implementation  Foo

-(void) test1{

         [self  test2];

}

-(void) test2{

         NSLog(@”test2is run”);

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息