Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning objects from methods in Objective-C

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

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

  1. Do I have to retain the returned object in Process method?
  2. Can I release *emp in Process function?

    -(void) Process { Employee *emp = [self GeEmployeetData] }

    +(Employee*) GeEmployeetData{

    Employee *emp = [[Employee alloc]init]; //fill entity

    return [emp autorelease]; }

like image 532
testuser Avatar asked Apr 07 '26 10:04

testuser


1 Answers

99% of the time you should retain autoreleased objects returned from other methods if you want to keep them around.

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.

like image 197
Jacob Relkin Avatar answered Apr 11 '26 04:04

Jacob Relkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!