从Objective-C中的方法返回对象 [英] Returning objects from methods in Objective-C

查看:73
本文介绍了从Objective-C中的方法返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请澄清一下,如何处理方法返回的对象?

Please clarify, how to deal with returned objects from methods?

下面,我从具有自动发布功能的GeEmployeetData函数中获取员工详细信息,

Below, I get employee details from GeEmployeetData function with autorelease,

  1. 我是否必须在Process方法中保留返回的对象?
  2. 我可以在Process函数中释放* emp吗?

  1. Do I have to retain the returned object in Process method?
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆