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: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2011/10/21/2219738.html
相关文章
|
2月前
|
编译器 程序员 API
C++ 14 17 新特性:[[fallthrough]], [[nodiscard]], [[maybe_unused]], 和 [[deprecated]] 新属性的使用...
C++ 14 17 新特性:[[fallthrough]], [[nodiscard]], [[maybe_unused]], 和 [[deprecated]] 新属性的使用...
20 3
UITextView根据NSString计算Size
UITextView根据NSString计算Size
43 0
|
小程序
微信小程序:TypeError: Cannot read property ‘mark‘ of undefined
微信小程序:TypeError: Cannot read property ‘mark‘ of undefined
134 0
微信小程序:TypeError: Cannot read property ‘mark‘ of undefined
|
Swift
Swift: UITextView&UITextField 限制字数和输入内容
Swift: UITextView&UITextField 限制字数和输入内容
1222 0
Swift: UITextView&UITextField 限制字数和输入内容
|
存储 自然语言处理 Java
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(二)
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(二)
220 0
|
机器学习/深度学习 API iOS开发
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
142 0
Object C学习笔记7-字符串NSString之一
  在Object C中存在两个类用于操作字符串,NSString和NSMutableString;NSString在赋值之后不能修改其内容和长度,而NSMutableString可以动态的修改字符串内容和长度,其主要区别就和.NET 中的string与StringBuilder之间的区别。
959 0
|
测试技术 索引
Object C学习笔记8-字符串NSString之二
  5. 字符串是否包含     hasPrefix 判断字符串是否以某个字符串开头     hasSuffix 判断字符串是否以某个字符串结尾 NSString *str1=@"Object C学习正在进行中.
1145 0
|
Windows
NSString的boolValue方法甚解
前言 NSString的boolValue之前有使用,但是一直没有真正了解什么时候返回YES(true)或NO(false)。其实,苹果在官方文档中已经写的很清楚,按command + control 点击boolValue进入文档就可以看到: boolValue The Boolean value of the string.
911 0
|
iOS开发
iOS学习笔记--tableView中如何获取cell上textfiled的值
iOS学习笔记--tableView中如何获取cell上textfiled的值 最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。
1447 0