从Objective-C中的方法返回对象 [英] Returning objects from methods in Objective-C
问题描述
请澄清一下,如何处理方法返回的对象?
Please clarify, how to deal with returned objects from methods?
下面,我从具有自动发布功能的GeEmployeetData函数中获取员工详细信息,
Below, I get employee details from GeEmployeetData function with autorelease,
- 我是否必须在Process方法中保留返回的对象?
-
我可以在Process函数中释放* emp吗?
- Do I have to retain the returned object in Process method?
Can I release *emp in Process function?
-(无效)过程 { 员工* emp = [self GeEmployeetData] }
-(void) Process { Employee *emp = [self GeEmployeetData] }
+(Employee *)GeEmployeetData {
+(Employee*) GeEmployeetData{
Employee * emp = [[Employee alloc] init]; //填充实体
Employee *emp = [[Employee alloc]init]; //fill entity
return [emp autorelease]; }
return [emp autorelease]; }
推荐答案
99%的时间,如果要保留其他方法返回的自动释放对象,则应保留它们.
99% of the time you should retain autoreleased objects returned from other methods if you want to keep them around.
对于自动释放的对象,当池耗尽时,将向池中的对象发送release
消息. 这就是为什么您有99%的时间要保留自动释放对象的原因,因为获得retainCount
大于1
的对象的可能性很小.
With autoreleased objects, when the pool is drained, the objects in the pool get sent the release
message. That is why 99% of the time you will want to retain autoreleased objects, because the chances of you getting an object with a retainCount
of more than 1
is highly unlikely.
这篇关于从Objective-C中的方法返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!