Category一些(实用)用法
2013-09-03 16:04
218 查看
--实现基本的类别扩展,用以修改属性的读写属性:
Things.h
--实现分布的实现类功能(多人开发/框架过大)
CategoryThing.h
--扩展系统的功能(最常见)
NSString+NumberConvertion.h
NSString+NumberConvertion.m
本文出自 “JAVA积累” 博客,请务必保留此出处http://linwb.blog.51cto.com/5577836/1287887
Things.h
#import <Foundation/Foundation.h> @interface Things : NSObject @property (assign) NSInteger Things1; @property (readonly, assign) NSInteger Things2; -(void)resetAllValue; @endThings.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; @endThing1.m
#import "CategoryThing.h" @implementation CategoryThing(Thing1) -(void)setThing1:(NSInteger)th1{ thing1=th1; }; -(NSInteger)thing1{ return thing1; }; @endThing2.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
相关文章推荐
- PullToRefresh一些实用的用法
- 六数码 广搜例题 附带一些实用的小用法
- SecureCRT(Linux平台)中的一些实用/有趣用法
- UIDevice的一些实用的Category
- 记录一些简单实用的vim用法
- Vim一些实用的用法
- (java)StringUtil类的一些用法,特别实用
- iBatis一些非见用法(相当实用)
- iBatis一些非见用法(相当实用)
- oracle 一些比较实用的用法
- list的一些常见用法总结(实用干货收藏)
- oracle 一些比较实用的用法
- Query Options的一些用法(1): 展开用户列信息
- dom4j的一些用法
- 一些有用的系统存储过程及用法
- [记忆向]工作中一些实用的Linux命令组合(持续)
- Apache Pig的一些基础概念及用法总结2(转)
- path的一些基本用法
- TCppWebBrowser控件的一些用法
- UITextField的一些用法