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 ,如需转载请自行联系原作者
相关文章
UITextView根据NSString计算Size
UITextView根据NSString计算Size
67 0
|
算法
Tips In C
C语言中的使用操作 宏定义时使用do while防止语句的分离, 但是不使用与需要有返回值的语句, 这个时候可以参考第二条 宏定义时使用({}), ()加上{}的方式, 在代码中填写逻辑算法, 最后的一条语句就是该宏定义的返回值; 在使用该宏定义时需要以";"结尾
815 0
NSString 简单细说
NSString 简单细说(一)—— NSString整体架构NSString 简单细说(二)—— NSString的初始化NSString 简单细说(三)—— NSString初始化NSString 简单细说(四)—— 从URL初始化NSString...
909 0
|
Windows
NSString的boolValue方法甚解
前言 NSString的boolValue之前有使用,但是一直没有真正了解什么时候返回YES(true)或NO(false)。其实,苹果在官方文档中已经写的很清楚,按command + control 点击boolValue进入文档就可以看到: boolValue The Boolean value of the string.
967 0
|
程序员 C++
|
SQL 数据库 C++