NSUerDefault中可以存储NSMutableadapter存储过程rray吗

objective C 一个简单的模仿NSMutableArray来说明内存分配机制
0人收藏此代码,
在OC中,使用数组的时候,往往会alloc一个对象后直接就release,这个的原因是在数组里面会调用retain方法来保持这个对象,所以需要release,基本上使用NSMutableArray对象的都会采用这个方法如下.
NSMutableArray *array = [[NSMutableArray alloc] init];
&span style=&white-space:pre&&
&/span&for(int i = 0; i & 4; i++){
Dog *d = [[Dog alloc] init];
[d setID:i];
NSLog(@&dog reatinCount is %ld&,[d retainCount]);
[array addObject:d];
NSLog(@&dog reatinCount is %ld&,[d retainCount]);
[d release];
[array release];
//该代码片段来自于: /codes/objectc/5456
然后下面我们将自己写一个简单的数组来模拟一下这个数组里面到底进行了什么样的活动,下面代码如下
#import &Foundation/Foundation.h&
@interface MyArray : NSObject
NSUInteger _//数组当前有几项元素
id _objs[512];//创建了一个512项的数组
@property (assign,readonly) NSUI
- (void) addObject: (id)
- (id) objectAtIndex: (NSUInteger)
- (void) removeObjectAtIndex : (NSUInteger)
- (void) removeA
//该代码片段来自于: /codes/objectc/5456
然后是实现文件
#import &MyArray.h&
@implementation MyArray
@synthesize count = _
- (id) init
self = [super init];
_count = 0;
- (void) addObject:(id)object
if(_count &512)
_objs[_count++] = [object retain];//这里必须retain保证数组里面存放的不是野指针,count++进行下一次存取准备
- (id) objectAtIndex:(NSUInteger)index
return _objs[index];
- (void) removeObjectAtIndex:(NSUInteger)index
id obj = _objs[index];
[obj release];//因为上面保存了,所以下面需要释放这个东西
_objs[index] =
- (void) removeAll
for(int i = 0; i & _ i++){
[self removeObjectAtIndex:i];
- (void) dealloc
NSLog(@&数组已经释放&);
[self removeAll];
[super dealloc];
//该代码片段来自于: /codes/objectc/5456
最后是使用这个文件
MyArray * array = [[MyArray alloc] init];
for(int i = 0; i & 4; i++){
Dog * d = [[ Dog alloc] init];
[d setID:i];
NSLog(@&dog reatinCount is %ld&,[d retainCount]);
[array addObject:d];
NSLog(@&dog reatinCount is %ld&,[d retainCount]);
[d release];
[array release];
//该代码片段来自于: /codes/objectc/5456
然后最后的运行结果如下
15:06:15.671 MyArray[] dog reatinCount is 1
15:06:15.673 MyArray[] dog reatinCount is 2
15:06:15.674 MyArray[] dog reatinCount is 1
15:06:15.674 MyArray[] dog reatinCount is 2
15:06:15.675 MyArray[] dog reatinCount is 1
15:06:15.676 MyArray[] dog reatinCount is 2
15:06:15.676 MyArray[] dog reatinCount is 1
15:06:15.677 MyArray[] dog reatinCount is 2
15:06:15.677 MyArray[] 数组已经释放
15:06:15.678 MyArray[] Dog id 0 dealloc
15:06:15.678 MyArray[] Dog id 1 dealloc
15:06:15.679 MyArray[] Dog id 2 dealloc
15:06:15.679 MyArray[] Dog id 3 dealloc
//该代码片段来自于: /codes/objectc/5456
相关代码片段:
最新Objective C代码片段
合作网站:查看:4751|回复:2
初级工程师
哪位知道?
初级工程师
数组只能存放指针类型的数据,所以int要转换成NSNumber类型来存储.
初级工程师
基本类型不能直接存,转成object: NSNumber *num = [NSNumber numberWithInt:10];
[array addObject:num];在关闭ARC情况下对NSMutableArray存储内存分析。
一:测试环境构建
1:创建一个简单视窗工程 在AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
ViewController *viewCtrl =[[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
UINavigationController *navigationCtrl = [[UINavigationController alloc] initWithRootViewController:(UIViewController*) viewCtrl];
self.viewController =
(ViewController*)navigationC
self.window.rootViewController = self.viewC
[navigationCtrl release];
[self.window makeKeyAndVisible];
return YES;
2 在ViewController.m中
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *rightItm = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(rightTarget:)];
self.navigationItem.rightBarButtonItem = rightI
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backB
[backButton release];
[rightItm release];
-(void)rightTarget:(id)sender
SecondViewController *secondViewCtrl = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondViewCtrl animated:YES];
[secondViewCtrl release];
3在&SecondViewController.h中申明如下:
NSMutableArray *arrayO
&SecondViewController.m中实现
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Custom initialization
[self.view setBackgroundColor: [UIColor grayColor] ];
[self initArrayData];
-(void)initArrayData
arrayObj = [[NSMutableArray alloc] initWithCapacity:0];
DataSorceViewController *dataViewCtrl =
for (int i = 0 ; i & 1024; i++)
dataViewCtrl = [[DataSorceViewController alloc] init];
[arrayObj addObject: dataViewCtrl];
-(void)viewWillDisappear:(BOOL)animated
for ( DataSorceViewController *data in arrayObj)
[data release];
[arrayObj removeAllObjects];
[arrayObj release];
arrayObj =
4 引入测试用的数据对象&DataSorceViewController,这里用的UIViewController子类,可以用任何类型的数据做为测试
二 :测试操作
1,SecondViewController.m 的&&-(void)viewWillDisappear:(BOOL)animated 中不做任何操作。用profile memeory leak测试,当页面切换操作后发现,有红色标注,表示有内存泄漏。
2.&-(void)viewWillDisappear:(BOOL)animated函数中加入&
[arrayObj release];
arrayObj =
切换页面,profile memeory leak中没有发现有红色标注,表示没有内存泄漏吗?其实不然,在Statistics的数据对象列表中,DataSorceViewController 对象始终存在,而且在多次切换操作中DataSorceViewController对象所占的内存每切换一次就会增长一次。多次切换后内存DataSorceViewController对象占用内存居高不下。
3:-(void)viewWillDisappear:(BOOL)animated函数中加入
[arrayObj removeAllObjects];
[arrayObj release];
arrayObj =
效果同第二步。
4:增加对数组对象释放操作
for ( DataSorceViewController *data in arrayObj)
[data release];
[arrayObj removeAllObjects];
[arrayObj release];
arrayObj =
在页面每次切换以后,DataSorceViewController 对象被释放。
一:profile memeory leak 检测,那个红色的标示并不是可靠的,即使有内存泄漏,leaks红色标示也可能没有出现
二:NSArray(NSMutableArray)数组,在&addObject 操作是会对对象做retain操作,同时 removeobject 操作会对对象做release操作,但数组中保存的对象所占的内存并没有释放,因为alloc操作时候retaincount 为1,一次addObject操作,retaincount 2,rimoveObject操作,retaincont为1,实际内存并没有得到释放。要真正释放内存,要遍历数组,对数组中保持的每个对象做release操作。 最后清除数组所有对象以及其本身。同理可得其它容器也是相同性质,比如NSDictionary等
三:以上为个人试验结论,如有不正确地方,欢迎指正!
阅读(...) 评论()

我要回帖

更多关于 er no such table 的文章

 

随机推荐