--實現基本的類別擴展,用以修改屬性的讀寫屬性:
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;
@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