oc为每个对象提供一个内部计数器,这个计数器跟踪对象的引用计数,当对象被创建或拷贝时,引用计数为1,每次保持对象时,调用retain接口,引用计数加1,如果不需要这个对象时调用release,引用计数减1,当对像的引用计数为0时,系统就会释放掉这块内存,释放对象调用dealloc
当对象包含其他对象时,就得在dealloc中自己释放他们
NSObject是
IOS所有类的基类
有两个基本函数,alloc和dealloc
alloc类似于C++的new,dealloc类似于delete
当对象的retaincount为0时,自动调用dealloc函数
release只是使retaincount-1,不是调用dealloc函数
内存管理的原则:
如果使用alloc,copy创建的对象,一定要release
如果你retain一个对象,那么必须要release
Song 类的实现
#import
@interface Song : NSObject
{
NSString *_title;
NSString *_artist;
long int _duration;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *artist;
@property (nonatomic,assign) long int duration;
-(Song*)initwithTitle:(NSString *)t AndArtist:(NSString *)art AndDuration:(long int)d;
@end
#import "Song.h"
@implementation Song
@synthesize title=_title;
@synthesize artist=_artist;
@synthesize duration=_duration;
-(Song*)initwithTitle:(NSString *)t AndArtist:(NSString *)art AndDuration:(long)d
{
self=[super init];
if(self)
{
self.title=t;
self.artist=art;
self.duration=d;
}
return self;
}
@end
main函数code
int main(int argc, const char * argv[])
{
Song *Song1=[[Song alloc] initwithTitle:@"what" AndArtist:@"hello" AndDuration:3];
Song *Song2=[[Song alloc] initwithTitle:@"aaa" AndArtist:@"bbb" AndDuration:4];
NSLog(@"Song1 retain count is %ld",[Song1 retainCount]);
NSLog(@"Song2 retain count is %ld",[Song2 retainCount]);
[Song1 retain];
[Song2 retain];
NSLog(@"Song1 retain count is %ld",[Song1 retainCount]);
NSLog(@"Song2 retain count is %ld",[Song2 retainCount]);
[Song1 release];
[Song2 release];
NSLog(@"Song1 retain count is %ld",[Song1 retainCount]);
NSLog(@"Song2 retain count is %ld",[Song2 retainCount]);
[Song1 release];
[Song2 release];
return 0;
}
the result:
2013-05-07 14:44:55.170 Access[2891:303] Song1 retain count is 1
2013-05-07 14:44:55.173 Access[2891:303] Song2 retain count is 1
2013-05-07 14:44:55.173 Access[2891:303] Song1 retain count is 2
2013-05-07 14:44:55.173 Access[2891:303] Song2 retain count is 2
2013-05-07 14:44:55.174 Access[2891:303] Song1 retain count is 1
2013-05-07 14:44:55.174 Access[2891:303] Song2 retain count is 1
内存管理释放池提供了一个对象容器,每次对象发送autorelease时,对象的引用计数并不真正变化,而是内存释放池记录一条记录,记下该对象的要求,直到内存释放池发送retain或release时,当池在销毁之前通知池内所有元素,发送release消息减1,这部分代码必须放在:
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
和 [pool release]; 之间
int main(int argc, const char * argv[])
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSArray *weeks1=[NSArray arrayWithObjects:@"monday",@"tuesday",@"thursday", nil];
NSArray *weeks2=[[NSArray alloc ]initWithObjects:@"monday",@"tuesday",@"thursday", nil];
//[weeks1 autorelease];
[weeks1 release];
[weeks2 release];
//[weeks2 autorelease];
NSLog(@"retain count is %ld",[weeks1 retainCount]);
NSLog(@"retain count is %ld",[weeks2 retainCount]);
[pool release];
return 0;
}
属性简介
@property 和@synthesize 可以自动生成某个类成员变量的存取方法,
语法 @property(参数) 类型 名字
这里的参数分为三大类:
读写属性:(readwrite/readonly) readwrite:这个属性是默认的,readonly:只生成getter 不会有setter
原子性(nonatomic)atomic ;是为了保证程序能够并发,避免同步问题
内存管理:(assign/retain/copy)
assign:这个属性用来处理基础类型,比如int,float,如果你声明的类型就是基础类型,该属性可以不加
对于assign而言,set函数和get函数如下所示:
@property(nonatomic,assign)int val;
-(int)val
{
return val;
}
(void)setVal:(int)newVal