Object - C memory manager 연습 및 결과 분석
코드:
//
// Dog.h
// HelloDog
//
// Created by jimzhai on 13-1-31.
// Copyright (c) 2013 jimzhai. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
@class Person;
@interface Dog : NSObject
{
int Id;
NSMutableArray *hostArray;
NSString *name;
}
@property int Id;
@property(assign) NSString* name;
-(void) addHost: (Person*)person;
-(void)printHosts;
-(void)deleteHost:(Person*) person;
@end//Dog
//
// Dog.m
// HelloDog
//
// Created by jimzhai on 13-1-31.
// Copyright (c) 2013 jimzhai. All rights reserved.
//
#import "Dog.h"
@implementation Dog
@synthesize Id = Id;
@synthesize name = name;
- (id)init
{
self = [super init];
if (self) {
hostArray = [NSMutableArray new];
hostArray = [NSMutableArray arrayWithCapacity:10];
[hostArray retain];//retainCount+1
}
return self;
}//init
-(void) addHost:(Person *)person
{
//[person retain];
[hostArray addObject:person];
}//addHost
-(void)printHosts
{
int count = 0;
for(id p in hostArray)
{
NSLog(@"%@",[p name]);
count++;
}
NSLog(@"totalHost: %d",count);
}//printHosts
-(void)deleteHost:(Person *)person
{
[hostArray removeObject:person];
}//deleteHost
- (void)dealloc
{
[hostArray release];
NSLog(@"%@ has been killed!",name);
[super dealloc];
}//dealloc
@end//Dog
//
// Person.h
// HelloDog
//
// Created by jimzhai on 13-1-31.
// Copyright (c) 2013 jimzhai. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@class Dog;
@interface Person : NSObject
{
NSString *name;
Dog *dog;
}
@property(assign) NSString *name;
@property(retain) Dog *dog;
@end
//
// Person.m
// HelloDog
//
// Created by jimzhai on 13-1-31.
// Copyright (c) 2013 jimzhai. All rights reserved.
//
#import "Person.h"
#import "Dog.h"
@implementation Person
@synthesize name = name;
@synthesize dog = dog;
- (void)dealloc
{
[dog release];
dog = nil;
NSLog(@"%@ dealloc!",name);
[super dealloc];
}
@end
// main.m
// HelloDog
//
// Created by jimzhai on 13-1-31.
// Copyright (c) 2013 jimzhai. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Dog *dog1 = [[Dog new] init];
[dog1 setId:89757];
[dog1 setName:@"good looking"];
NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
Person *xiaoli = [Person alloc];
NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
[xiaoli setName:@"xiaoli"];
[xiaoli setDog:dog1];
[dog1 addHost:xiaoli];
NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
Person *laowang = [Person alloc];
NSLog(@"laowang retainCount: %ld",[laowang retainCount]);
[laowang setName:@"laowang"];
[laowang setDog:dog1];
[dog1 addHost:laowang];
NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
NSLog(@"laowang retainCount: %ld",[laowang retainCount]);
[dog1 printHosts];
[dog1 deleteHost:xiaoli];
[xiaoli release];
NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
[dog1 printHosts];
NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
[dog1 deleteHost:laowang];
[laowang release];
[dog1 release];
}
return 0;
}//main
결과:
2013-01-31 18:12:12.782 HelloDog[3603:303] dog1 retainCount: 1
2013-01-31 18:12:12.784 HelloDog[3603:303] xiaoli retainCount: 1
2013-01-31 18:12:12.794 HelloDog[3603:303] dog1 retainCount: 2
2013-01-31 18:12:12.798 HelloDog[3603:303] xiaoli retainCount: 2
2013-01-31 18:12:12.803 HelloDog[3603:303] laowang retainCount: 1
2013-01-31 18:12:12.805 HelloDog[3603:303] dog1 retainCount: 3
2013-01-31 18:12:12.806 HelloDog[3603:303] laowang retainCount: 2
2013-01-31 18:12:12.807 HelloDog[3603:303] xiaoli
2013-01-31 18:12:12.807 HelloDog[3603:303] laowang
2013-01-31 18:12:12.808 HelloDog[3603:303] totalHost: 2
2013-01-31 18:12:12.809 HelloDog[3603:303] xiaoli dealloc!
2013-01-31 18:12:12.809 HelloDog[3603:303] xiaoli retainCount: 1
2013-01-31 18:12:12.809 HelloDog[3603:303] laowang
2013-01-31 18:12:12.810 HelloDog[3603:303] totalHost: 1
2013-01-31 18:12:12.810 HelloDog[3603:303] dog1 retainCount: 2
2013-01-31 18:12:12.811 HelloDog[3603:303] laowang dealloc!
2013-01-31 18:12:12.811 HelloDog[3603:303] good looking has been killed!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.