按照网上的方法安装环境,在csdn下载相应软件很快搞定,写了测试程序如下:
分开一个一个文件写编译不了,应该是要写连接编译参数等的东西,不懂!所以写在同一个文件是最简单的,文件命名为helloworld.m
编译命令:gcc -o NilTest NilTest.m -I/GNUstep/System/Library/Headers -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base
环境按这篇文章搭建:
http://blog.csdn.net/zajin/article/details/9133793
#import <Foundation/Foundation.h>
@interface XYPoint:NSObject{
int x;
int y;
}
-(id)initWithX:(int) _x andY:(int) _y;
-(void) dealloc;
@end
@implementation XYPoint
-(id)initWithX:(int) _x andY:(int)_y{
x = _x;
y = _y;
}
-(void) dealloc {
NSLog(@"Point is dealloc!");
return [super dealloc];
}
@end
@interface Circle:NSObject{
int r;
XYPoint* p;
}
-(void)print;
-(void)setP:(int) _x :(int) _y;
-(XYPoint*)getP;
@end
@implementation Circle
-(void) print{
NSLog(@"radius is : %d point is : %@ isa is : %@",r,p,isa);
NSLog(@"point retaincount is : %d",[p retainCount]);
}
-(void) setP:(int) _x :(int) _y{
[p release];
p = [[XYPoint alloc] initWithX:_x andY:_y];
}
-(XYPoint*) getP{
return p;
}
@end
int main (int argc, const char*argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
Circle* c = [[Circle alloc] init];
[c setP:10 :20];
//[[c getP] release];//看看是否会调用Point的dealloc,会调用dealloc方法
//[[c getP] retainCount];//windows环境下报内存不能为read,因为p已经释放
//[[c getP] release];//连续两次release,报内存不能为read,因为p已经释放,释放后就是销毁了,不是Nil空值。
[c print];
[pool drain];
return 0;
}