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
相关文章推荐
- Objective-C中的协议(Protocol)和类别(Category)
- Objective-C( Category 分类,非正式协议,分类延展)
- Objective-C分类 (category),扩展(Extension)和协议(protocol)
- Objective-C category (类别) 和 protocol (协议)
- Objective-C 【Category-非正式协议-延展】
- Objective-C 【Category-非正式协议-延展】
- Objective-C-----协议protocol,代码块block,分类category
- Objective-C-----协议protocol,代码块block,分类category
- Objective-C:继承、分类(Category、extension)、协议(protocol),个人理解,仅供参考
- Objective-C 日记⑤ 内存管理、协议、Category 视频笔记
- 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解
- Objective-C——Message(消息)、Category(分类) 、Protocol(协议) 总结
- Objective-C Runtime 解析(四)—— 在Category中添加属性
- Objective-C中的类别(Category)的使用
- 使用_ObjectiveCBridgeable协议实现Objective-C类与Swift结构体的无缝互转
- Objective-C学习笔记 利用协议实现回调函数
- Objective-C 协议(protocol)
- 深入理解Objective-C:Category(上)
- 详解Objective-C中委托和协议
- Objective-c Category(类别)