6. 实现类:
@implementation MemFoo
@synthesize x;
@synthesize name;
//重载的实现
-(MemFoo*)init:(int)n:(NSString*)str
{
self=[super init]; //先使用父类方法进行初始化
if (self!=NULL) {
self.x=n;
self.name=str;
}
return self;
}
@end
调用该类:
in t m ain (in t arg c , c o n s t c h ar * arg v[ ] ) {
N S Au to re le as e Po o l * p o o l = [ [ N S Au to re le as e Po o l allo c ] in it] ;
Me m Fo o * m yMe m = [ [ Me m F o o allo c ] in it:5:@ "h e llo "] ;
[ m yMe m re le as e ] ;
} 6
10. @implementation MemFoo
@synthesize x;
@synthesize name;
//重载的实现
-(MemFoo*)init:(int)n:(NSString*)str
{
self=[super init]; //先使用父类方法进行初始化
if (self!=NULL) {
self.x=n;
self.name=str;
}
return self;
}
@end
in t m ain (in t arg c , c o n s t c h ar * arg v[ ] ) {
N S Au to re le as e Po o l * p o o l = [ [ N S Au to re le as e Po o l allo c ] in it] ;
Me m Fo o * m yMe m = [ [ Me m F o o allo c ] in it:5:@ "h e llo "] ;
[ m yMe m au to re le as e ] ;
N S Lo g (@ "00000000 x is = % x, n am e is = % @ ",[ m yMe m x] ,[ m yMe m n am e ] );
[ p o o l d rain ] ;
re tu rn 0; 10
}
14. 9. 文件类的各种操作方法
NSString *fName=@"testfile.m";
NSFileManager *fm=NULL;
NSDictionary *dict=NULL;
fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:fName]==NO) {
NSLog(@"file not exist!");
return 1;
}
if ([fm copyPath:fName toPath:@"newfile" handler:nil]==NO) {
NSLog(@"copy failed!");
}
if ([fm movePath:@"newfile" toPath:@"newfolder" handler:nil]==NO) {
NSLog(@"move failed");
}
if ((dict=[fm fileAttributesAtPath:@"newfolder" traverseLink:NO])==nil) {
NSLog(@"get file attributes failed");
}
NSLog(@"file size is %i bytes",[[dict objectForKey:NSFileSize] intValue]);
if ([fm removeFileAtPath:@"newfolder" handler:nil]==YES) {
NSLog(@"remove successful!");
}
14
15. 10. 协议
正式协议 (p ro to c al)
其实就是非正式协议 (in te rfac e ) 换了一种写法而已,看上去更正规一些,语义上更强
烈一些:要求采用该协议的类,”必须”实现协议中约定的方法。但是比较娱乐的是
,即使是号称正式协议,编译器在编译时,遇到不守规矩的情况,仍然只是给出警告。
(当然正式协议也有它存在的意义,后面会提到)
这里我们定义一个 IQ u e ry 的协议
协议:
@ p ro to c o l IQ u e ry
- (vo id ) Q u e ry:(N S S trin g * ) s q l;
@ e nd
头文件类:
@ in te rfac e D B Q u e ry : N S O b j c t< IQ u e ry> {
e
}
@ e nd
实现类:
@ im p le m e n tatio n D B Q u e ry
- (vo id ) Q u e ry:(N S S trin g * )s q l
{
N S Lo g (@ ”Q u e ry is c alle d . s q l:% @ ”,s q l);
}
@ e nd 15
21. 11. 内存管理
自动释放池,在 O b j c tive - C 模块中,发挥着重要作用,能够帮助我们清理很多内存。但是它也
e
不是万能的,也必须要合理小心的使用。
本文通过一个示例代码,来介绍下自动释放池的用法和注意事项。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MemFoo *pMem=[[MemFoo alloc]init];
NSLog(@"aaaaa retaincount: %d", [pMem retainCount]);
[pool drain];
pool=[[NSAutoreleasePool alloc]init];
[pMem autorelease]; //不会增加引用计数
NSLog(@"bbbbb retaincount: %d",[pMem retainCount]);
[pMem retain];
NSLog(@"ccccc retaincount: %d",[pMem retainCount]);
[pool drain];
NSLog(@"ddddd retaincount: %d",[pMem retainCount]);
[pMem release]; //必须要自己主动释放一次
运行结果如下所示:
201 1 - 03- 22 23:39:1 8.1 85 Me m o ryTe s t[ 478:a0f] aaaaa re tain c o u n t: 1
201 1 - 03- 22 23:39:1 8.1 87 Me m o ryTe s t[ 478:a0f] bbbbb re tain c o u n t: 1
201 1 - 03- 22 23:39:1 8.1 88 Me m o ryTe s t[ 478:a0f] ccccc re tain c o u n t: 2
201 1 - 03- 22 23:39:1 8.1 88 Me m o ryTe s t[ 478:a0f] ddddd re tain c o u n t: 1
21
所以,除了依赖于自动释放池外,对于额外的增加引用,必须由自己去亲自释放。