您的位置:首页 > 编程语言 > Go语言

Category一些(实用)用法

2013-09-03 16:04 218 查看
--实现基本的类别扩展,用以修改属性的读写属性:
Things.h
#import <Foundation/Foundation.h>
@interface Things : NSObject
@property (assign) NSInteger Things1;
@property (readonly, assign) NSInteger Things2;
-(void)resetAllValue;
@end
Things.m
#import "Things.h"
@interface Things()
@property (assign) NSInteger Things3;
@property (readwrite, assign) NSInteger Things2;
@end
@implementation Things
@synthesize Things1;
@synthesize Things2;
@synthesize Things3;
-(void)resetAllValue{
self.Things1=1;
self.Things2=2;
self.Things3=2;
};
-(NSString *)description{
return ([NSString stringWithFormat:@"Things1=%i,Things2=%i,Things3=%i",self.Things1,self.Things2,self.Things3]);
}
@end
对于一些隐藏细节的属性,通常需要使用类别来实现代码的读写控制,如该类中的Things2属性。

--实现分布的实现类功能(多人开发/框架过大)
CategoryThing.h

@interface CategoryThing : NSObject{
NSInteger thing1;
NSInteger thing2;
NSInteger thing3;
}
@end
@interface CategoryThing(Thing1)
-(void)setThing1:(NSInteger)th1;
-(NSInteger)thing1;
@end
@interface CategoryThing(Thing2)
-(void)setThing2:(NSInteger)th2;
-(NSInteger)thing2;
@end
@interface CategoryThing(Thing3)
-(void)setThing3:(NSInteger)th3;
-(NSInteger)thing3;
@end
Thing1.m
#import "CategoryThing.h"
@implementation CategoryThing(Thing1)
-(void)setThing1:(NSInteger)th1{
thing1=th1;
};
-(NSInteger)thing1{
return thing1;
};
@end
Thing2.m Thing3.m以此类推。

--扩展系统的功能(最常见)
NSString+NumberConvertion.h
@interface NSString (NumberConvertion)
-(NSNumber *)lengthAsNumber;
@end


NSString+NumberConvertion.m

#import "NSString+NumberConvertion.h"
@implementation NSString (NumberConvertion)
-(NSNumber *)lengthAsNumber{

NSUInteger length=[self length];
return ([NSNumber numberWithUnsignedInteger:length]);

};
@end


本文出自 “JAVA积累” 博客,请务必保留此出处http://linwb.blog.51cto.com/5577836/1287887
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: