//
// main.m
// 多个对象的内存管理
//
// Created by admin on 15/8/5.
// Copyright (c) 2015年 admin. All rights reserved.
//
#import <Foundation/Foundation.h>
// 人拥有一辆汽车
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1
Person * p = [[Person alloc] init];
// 1
Car * car = [[Car alloc] init];
//2
[p setCar:car];
[p drive];
//1
[car release];
//要保证对象p 存在的时候,调用对象p的任何方法都不会出错
[p drive];
//0
[p release];
}
return 0;
}
//
// Person.h
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import <Foundation/Foundation.h>
#import "Car.h"
@interface Person : NSObject
{
Car * _car;
}
- (void)setCar:(Car *)car;
- (Car *)car;
- (void)drive;
@end
//
// Person.m
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import "Person.h"
@implementation Person
- (void)setCar:(Car *)car
{
//2,能够使得retainCount + 1 并且返回self
_car = [car retain];
}
- (Car *)car
{
return _car;
}
- (void)drive
{
[_car run];
}
- (void)dealloc
{
//目的是要保证在p对象存在的时候,car对象一定存在
//对象p被销毁的时候,
[_car release]
[super dealloc];
NSLog(@"Person 被销毁了");
}
@end
//
// Car.h
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import <Foundation/Foundation.h>
@interface Car : NSObject
- (void)run;
@end
//
// Car.m
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import "Car.h"
@implementation Car
- (void)dealloc
{
[super dealloc];
NSLog(@"Car 被销毁了");
}
- (void)run
{
NSLog(@"汽车跑起来了");
}
@end
// main.m
// 多个对象的内存管理
//
// Created by admin on 15/8/5.
// Copyright (c) 2015年 admin. All rights reserved.
//
#import <Foundation/Foundation.h>
// 人拥有一辆汽车
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1
Person * p = [[Person alloc] init];
// 1
Car * car = [[Car alloc] init];
//2
[p setCar:car];
[p drive];
//1
[car release];
//要保证对象p 存在的时候,调用对象p的任何方法都不会出错
[p drive];
//0
[p release];
}
return 0;
}
//
// Person.h
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import <Foundation/Foundation.h>
#import "Car.h"
@interface Person : NSObject
{
Car * _car;
}
- (void)setCar:(Car *)car;
- (Car *)car;
- (void)drive;
@end
//
// Person.m
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import "Person.h"
@implementation Person
- (void)setCar:(Car *)car
{
//2,能够使得retainCount + 1 并且返回self
_car = [car retain];
}
- (Car *)car
{
return _car;
}
- (void)drive
{
[_car run];
}
- (void)dealloc
{
//目的是要保证在p对象存在的时候,car对象一定存在
//对象p被销毁的时候,
[_car release]
[super dealloc];
NSLog(@"Person 被销毁了");
}
@end
//
// Car.h
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import <Foundation/Foundation.h>
@interface Car : NSObject
- (void)run;
@end
//
// Car.m
// 01-手动内存管理基本概念
//
// Created by apple on 14-3-17.
//
//
#import "Car.h"
@implementation Car
- (void)dealloc
{
[super dealloc];
NSLog(@"Car 被销毁了");
}
- (void)run
{
NSLog(@"汽车跑起来了");
}
@end