NSString的一点tips

简介:

I have the following method

   -(NSMutableArray *) getPaises {
     
NSMutableArray * paises;
     paises
= [[NSMutableArray alloc] init];
     
while( get new row ) {
     
NSString *aPais =  [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
     
[paises addObject:aPais];
     
}
     
return paises;
   
}

I am not releasing the aPais, because if I do it the application crashes. I don't know when or if whether I should release it somewhere after using it and, if so, how do I do it. Just release the NSMutableArray is enough? Or do I have to traverse it and release each object?

And if I don't have to release it, who is the responsible for releasing?

link | improve this question


A note regarding method naming: In Cocoa, a method named “getFoo” returns foo by reference: - (void) getFoo:(out NSMutableArray **)outArray. To be consistent with Cocoa naming conventions, you should name your method simply “paises”. – Peter Hosey Mar 2 '09 at 18:13
feedback

2 Answers

up vote  6  down vote accepted

As epatel said, you don't need to release that particular string. If you wanted to be more proactive, you could do this instead:

-(NSMutableArray *) getPaises {
   
NSMutableArray * paises;
    paises
= [[[NSMutableArray alloc] init] autorelease];
   
while( get new row ) {
       
NSString *aPais =  [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
       
[paises addObject:aPais];
       
[aPais release];
   
}
   
return paises;
}

In summary:

  • [[NSString alloc] initWith...] -> You must release or autorelease.

  • [NSString stringWith...] -> No need to release.

-- Edit: Added autorelease for paises, as you are returning it. When you return an object, always autorelease it if you have alloc&init'd it.

link | improve this answer


Thanks a lot. I'm releasing the NSMutableArray manually, but the autorelease is a better option. Gonna change it. – Sacha Fuentes Mar 2 '09 at 14:30
feedback

stringWithUTF8String: returns an autorelease string which will be released automatically by Cocoa in the next eventloop. But the string is also retained in the array when you do addObject:...so as long as it is in the array it will be retained.

link | improve this answer

Was this post useful to you?     
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!













本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/10/21/2219738.html ,如需转载请自行联系原作者
相关文章
|
程序员 iOS开发 开发者
iOS开发:设置UICollectionView不同大小的item的方法
在iOS开发过程中,UICollectionView的使用作为iOS开发者来说都不陌生,但是要想完美的玩转UICollectionView的所有使用的技巧,还是需要了解很多的。本篇博文来分享一下关于UICollectionView设置不同大小item的方法,为的是迎合产品的需求,方便记录为了以后查看使用,分享给有需要的人。
867 0
iOS开发:设置UICollectionView不同大小的item的方法
UITextView根据NSString计算Size
UITextView根据NSString计算Size
63 0
Object C学习笔记7-字符串NSString之一
  在Object C中存在两个类用于操作字符串,NSString和NSMutableString;NSString在赋值之后不能修改其内容和长度,而NSMutableString可以动态的修改字符串内容和长度,其主要区别就和.NET 中的string与StringBuilder之间的区别。
1002 0
|
算法
Tips In C
C语言中的使用操作 宏定义时使用do while防止语句的分离, 但是不使用与需要有返回值的语句, 这个时候可以参考第二条 宏定义时使用({}), ()加上{}的方式, 在代码中填写逻辑算法, 最后的一条语句就是该宏定义的返回值; 在使用该宏定义时需要以";"结尾
810 0
|
Windows
NSString的boolValue方法甚解
前言 NSString的boolValue之前有使用,但是一直没有真正了解什么时候返回YES(true)或NO(false)。其实,苹果在官方文档中已经写的很清楚,按command + control 点击boolValue进入文档就可以看到: boolValue The Boolean value of the string.
961 0
|
程序员 C++
|
SQL 数据库 C++