objective-c autorelease

后端 未结 2 1703
情话喂你
情话喂你 2021-01-27 01:35

Im new to obj-c and have trouble understanding the function autorelease. could someone explain to me when i should use it? and how is it different than release. also do I need t

2条回答
  •  一个人的身影
    2021-01-27 02:15

    Calling autorelease schedules a release message to be sent to an object sometime in the near future by adding the object to the topmost NSAutoreleasePool. When a pool receives the drain message, it sends release to all the objects that have been added to it.

    autorelease is used in situations where a method or function needs to relinquish its ownership of an object, but needs to keep it from being deallocated temporarily so that its caller can do something with it. It's also useful in creating "convenience" methods that wrap alloc, initWith... and autorelease to make code that allocates objects simpler.

提交回复
热议问题