English 中文(简体)
苹果内存管理编程指南/硬示例
原标题:Apple Memory Management Programming Guide / Hard example

作为许多objc开发人员,我最终反复阅读了《苹果内存管理编程指南》,因为这是理解保留/释放/自动释放机制的最佳方式。

在这一页中,有一个我不理解的例子:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539-开关1

大宗报价

技术1

在技术1中,getter返回的值在调用范围内自动释放:

- (NSString*) title {

return [[title retain] autorelease];

}

- (void) setTitle: (NSString*) newTitle {

if (title != newTitle) {

    [title release];

    title = [newTitle retain]; // Or copy, depending on your needs.

}

}

因为从get访问器返回的对象在当前作用域中是自动释放的,所以如果属性值发生更改,它仍然有效。这使得访问器更加健壮,但代价是额外的开销。如果期望频繁调用getter方法,那么保留和自动释放对象所增加的成本可能不值得付出性能成本。

技术2

与技术1一样,技术2也使用自动释放技术,但这次是在setter方法中使用:

- (NSString*) title {

return title;

}

- (void) setTitle: (NSString*) newTitle {

[title autorelease];

title = [newTitle retain]; // Or copy, depending on your needs.

}

在getter比setter更频繁地被调用的情况下,技术2的性能明显优于技术1。

大宗报价

好吧,经过几分钟的激烈思考,我终于明白了苹果公司的说法,以及拥有[[title retain]autorelease]的原因。但是,如果我想合成它,我如何为setter指定它呢?我只知道setter的retain或copy as访问器方法。例如:

@property (retain) NSString* title

每次我为title设置新值时,都会保留title。但是,如何指定getter将返回[[title retain]autorelease],如示例一所示?它是通过XCode以这种方式隐式合成的吗?

Regards, Apple92

问题回答

如果您使用的是@synthesis,那么您不需要关心getter和setter实现,只要您正确地指定了属性选项(retain、copy等)。编译器会为您做正确的事情。

除非您看到关于getter和setter的实际性能问题(这是值得怀疑的),否则我不会担心它是如何在内部实现的


如果您真的想使用特定的getter或setter,只需在您的类上实现它。即使您有一个@synthesis语句,也会使用您的实现。或者,您可以同时实现getter和setter,并完全省略@synthesis语句。


或者您可以在属性声明中指定getter或setter访问器方法名





相关问题
Windows Mobile 6 Emulator change storage?

How do i change the size of the Windows Mobile 6 Emulator. Its fixed at 32mb. I read this post: Increasing Windows Mobile 5 Emulator Storage But it only helps for the 5.0 version. Isnt there any way ...

CUDA Memory Allocation accessible for both host and device

I m trying to figure out a way to allocate a block of memory that is accessible by both the host (CPU) and device (GPU). Other than using cudaHostAlloc() function to allocate page-locked memory that ...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a ...

Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I m not so sure. My assumption is (a) that an object I get from a function (dateFromComponents: ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

Objective-C returning alloc d memory in a function == bad?

This is on the iPhone. So what if I have a function like - (SomeObject*)buildObject; Do I need to pass in a variable that I have already alloc d outside like - (void)assignObject(SomeObject** out);...

热门标签