#import <Foundation/Foundation.h>
//类别
@interface NSString(MyAdditions)
+(NSString *)getCopyRightString;
@end
@implementation NSString(MyAdditions)
+(NSString *)getCopyRightString{
return @"Copyright TutorialsYiibai.com 2016";
}
@end
//扩展
@interface SampleClass : NSObject
{
// NSString *name;
int _age;
}
- (void)setInternalID;
- (NSString *)getExternalID;
@end
//扩展
@interface SampleClass()
{
NSString *internalID;
}
@end
@implementation SampleClass
- (void)setInternalID{
internalID = [NSString stringWithFormat:
@"UNIQUEINTERNALKEY%dUNIQUEINTERNALKEY",arc4random()%100];
}
- (NSString *)getExternalID{
return [internalID stringByReplacingOccurrencesOfString:
@"UNIQUEINTERNALKEY" withString:@""];
}
@end
//类别与扩展
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *copyrightString = [NSString getCopyRightString];
NSLog(@"Accessing Category: %@",copyrightString);
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass setInternalID];
NSLog(@"ExternalID: %@",[sampleClass getExternalID]);
[pool drain];
return 0;
}
objective-c之类别与扩展
最新推荐文章于 2021-10-27 21:19:39 发布