第五讲:Obj-C 内存管理3 - 自定义MyArray

来源:互联网 发布:巴比馒头 知乎 编辑:程序博客网 时间:2024/05/19 13:57

转:http://tigercat1977.blog.163.com/blog/static/2141561122012111294042162/

第五讲:Obj-C 内存管理3 - 自定义MyArray 

2012-12-12 09:40:42|  分类:Objective-C |  标签:objective-c |字号订阅

主要内容
      了解 NSMutableArray 如何实现
      进一步深入内存管理
      进一步深入内存管理!
      进一步深入内存管理!!
      进一步深入内存管理!!!

NSMutableArray 典型用法
      NSMutableArray *array = [[NSMutableArray alloc] init];
      for (int i = 0; i < 10; i++)
      {
            Dog *dog = [[Dog alloc] init];
            [dog setID:i];
            [array addObject:dog];
            [dog release];
      }
      for (Dog *d in array)
      {
            NSLog(@"dog is %d", [d ID]);
      }
      [array release];

MyArray 提供方法
      - (NSInteger) addObject:(id)object;     // 往类里面加个对象                  
      - (id) objectAtIndex:(NSUInteger) index;    // 取得第几项的对象
      - (void) removeObjectAtIndex:(NSUInteger) index;  // 删掉某一项
      - (void) removeAll;   //删掉所有的对象

举例子 (四条狗)

// Dog.h#import <Foundation/Foundation.h>@interface Dog : NSObject{ int _ID;}@property (assign) int ID;@end

// Dog.m#import "Dog.h"@implementation Dog@synthesize ID = _ID;// 当对象计数器到 0 的时候 自动会调用- (void) dealloc{ NSLog(@"dog id %d dealloc", _ID); [super dealloc];}@end

// main.m#import <Foundation/Foundation.h>#import "Dog.h"int main (int argc, const char * argv[]){ @autoreleasepool { NSLog(@"Hello, World!"); NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i =0; i < 4; i++) { Dog *d = [[Dog alloc] init]; [d setID:i]; NSLog(@"dog%i retainCount1 %ld", i, [d retainCount]); [array addObject:d]; NSLog(@"dog%i retainCount2 %ld", i, [d retainCount]); [d release]; } [array release]; } return 0;}/* 输出结果: Hello, World! dog0 retainCount1 1 dog0 retainCount2 2 dog1 retainCount1 1 dog1 retainCount2 2 dog2 retainCount1 1 dog2 retainCount2 2 dog3 retainCount1 1 dog3 retainCount2 2 dog id 0 dealloc dog id 1 dealloc dog id 2 dealloc dog id 3 dealloc*/


×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
举例子 (用 MyArray 提供方法)

// Dog.h#import <Foundation/Foundation.h>@interface Dog : NSObject{ int _ID;}@property (assign) int ID;@end

// Dog.m#import "Dog.h"@implementation Dog@synthesize ID = _ID;// 当对象计数器到 0 的时候 自动会调用- (void) dealloc{ NSLog(@"dog id %d dealloc", _ID); [super dealloc];}@end


// MyArray.h#import <Foundation/Foundation.h>@interface MyArray : NSObject{ NSUInteger _count; // 数组当前有几项元素 id _objs[512]; // 创建了一个 512 项的数组 4*512 }@property (assign, readonly) NSUInteger count;// 往 MyArray 中加入一项元素- (void) addObject:(id) object;// 取得第 index 项元素- (id) objectAtIndex:(NSUInteger) index;// 删除第 index 项元素- (void) removeObjectAtIndex:(NSUInteger) index;// 删除所有元素- (void) removeAll;@end

// MyArray.m#import "MyArray.h"@implementation MyArray@synthesize count = _count;- (id) init{ self = [super init]; if (self) { _count = 0; } return self;}- (void) addObject:(id) object{ if (_count >= 512) return; _objs[_count] = [object retain]; _count++; // 这里必须要做 [object retain];}- (id) objectAtIndex:(NSUInteger)index{ return _objs[index];}- (void) removeObjectAtIndex:(NSUInteger)index{ id obj = _objs[index]; [obj release]; _objs[index] = nil;}- (void) removeAll{ for (int i =0; i < _count; i++) { [self removeObjectAtIndex:i]; }}- (void) dealloc{ // 在数组对象被 release 到 0 时候调用 NSLog(@"before remove all"); [self removeAll]; NSLog(@"after remove all"); [super dealloc];}@end


// main.m#import <Foundation/Foundation.h>#import "Dog.h"#import "MyArray.h"int main (int argc, const char * argv[]){ @autoreleasepool { NSLog(@"Hello, World!"); MyArray *myArray = [[MyArray alloc] init]; for (int i = 0; i < 4; i++) { Dog *d = [[Dog alloc] init]; [d setID:i]; NSLog(@"dog%i retainCount1 is %ld", i, [d retainCount]); [myArray addObject:d]; NSLog(@"dog%i retainCount2 is %ld", i, [d retainCount]); [d release]; } [myArray release]; } return 0;}/* 输出结果: Hello, World! dog0 retainCount1 1 dog0 retainCount2 2 dog1 retainCount1 1 dog1 retainCount2 2 dog2 retainCount1 1 dog2 retainCount2 2 dog3 retainCount1 1 dog3 retainCount2 2 dog id 0 dealloc dog id 1 dealloc dog id 2 dealloc dog id 3 dealloc*/


(本讲完)

 

原创粉丝点击