Objective-C -编写简单方法的最佳实践?

时间:2022-12-30 20:06:50

In the following function which one is the best practice?

在下面的函数中,哪一个是最佳实践?

  1. To send an autoreleased object, and make the caller retain it?

    发送一个自动上传的对象,并让调用者保留它?

  2. or send an allocated object, and make the caller release it?

    或者发送一个已分配的对象,让调用者释放它?


- (NSString*) convertDataToString :(NSData*)myData
{
     //just an example, method might not exist
     NSString *str = [[NSString alloc] initWithData:myData];
     return str;
     return [str autoRelease];
}

6 个解决方案

#1


3  

The memory management rules say your first example is — and this is a direct quote — wrong. It's not even a matter of preference, as some answers here seem to indicate. The caller does not normally own the object you return, so it should be autoreleased.

内存管理规则说你的第一个例子是-这是直接引用-错误。这甚至不是一个偏好的问题,这里的一些答案似乎表明了这一点。调用者通常不拥有您返回的对象,因此它应该是自动加载的。

The specific example from the rules says this:

来自规则的具体例子是这样的:

This is wrong. Following the ownership policy, it would result in a memory leak.

这是错误的。按照所有权策略,它将导致内存泄漏。

 – (NSArray *)sprockets {

    NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,
                               auxiliarySprocket, nil];
    return array;
}

The object’s reference to the new array object is limited to the sprockets method. After the method returns, the object loses its reference to the new object so cannot relinquish ownership. That in itself is not a problem. However, following the naming convention set out earlier, the caller is given no indication that it owns the returned object. The caller would therefore not relinquish ownership of the returned object, leading to a memory leak.

对象对新数组对象的引用仅限于spro佝偻病方法。方法返回后,对象失去对新对象的引用,因此不能放弃所有权。这本身不是问题。但是,按照前面的命名约定,调用方没有表示它拥有返回的对象。因此,调用方不会放弃返回的对象的所有权,导致内存泄漏。

#2


7  

Following up on @Chuck's comment, -convertDataToString must not return an object that the caller must release. That would violate the Three Magic Words. If you do not have "copy," "alloc," or "new" in your name, the caller cannot be expected to release the object. If you have "copy" in your name or start with "new" or "alloc," then the caller must release the object.

根据@Chuck的评论,-convertDataToString不能返回调用者必须释放的对象。这就违背了三个神奇的词。如果您的名字中没有“copy”、“alloc”或“new”,则不能期望调用者释放对象。如果您的名字中有“copy”,或者以“new”或“alloc”开头,那么调用者必须释放对象。

Objective-C relies heavily on consistent naming and the names mean things. If you learn the naming, then you won't have any problems.

Objective-C很大程度上依赖于一致的命名和名字的含义。如果你学会了命名,就不会有任何问题。

#3


2  

You'd want to return an autoreleased object most of the time. Unless your method name contains one of the following words [alloc, new, copy], you should return an autoreleased object.

您可能希望在大多数情况下返回一个自动上传的对象。除非您的方法名称包含下列单词之一[alloc, new, copy],您应该返回一个autorele对象。

#4


1  

If you create, alloc, or copy an object, you are responsible for releasing it. Based on this, you should return an autoreleased object.

如果您创建、alloc或复制一个对象,您将负责释放它。基于此,您应该返回一个自动上传的对象。

#5


0  

I prefer to return the autorelease. It means that you aren't hunting around trying to find where memory is being freed. Keeping memory allocation and deallocation together makes your life easier. After all, you're coding this, why make it harder on yourself.

我更愿意退货。这意味着你并不是在四处搜寻,试图找到释放记忆的地方。保持内存分配和重新分配,使您的生活更轻松。毕竟,你在编码这个,为什么让它对你自己更困难。

#6


0  

Both are acceptable, but you should name your method accordingly : if the caller has the responsibility to deallocate it, you have to make this explicit by having your method name contain "create", "alloc" or "copy", else it should not. More reading on this at http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH

两者都是可以接受的,但是您应该相应地命名您的方法:如果调用者有责任释放它,那么您必须让您的方法名包含“create”、“alloc”或“copy”,以使其显式,否则它不应该。在http://developer.apple.com/library/mac/#文档/cocoa/概念性/MemoryMgmt/ article /mmRules.html%23/ apple_ref/doc/uid/20000994-BAJHFBGH上有更多阅读

It might be a bit more customary to return an autorelease object, but both are okay.

返回一个autorelease对象可能有点习惯,但两者都可以。

#1


3  

The memory management rules say your first example is — and this is a direct quote — wrong. It's not even a matter of preference, as some answers here seem to indicate. The caller does not normally own the object you return, so it should be autoreleased.

内存管理规则说你的第一个例子是-这是直接引用-错误。这甚至不是一个偏好的问题,这里的一些答案似乎表明了这一点。调用者通常不拥有您返回的对象,因此它应该是自动加载的。

The specific example from the rules says this:

来自规则的具体例子是这样的:

This is wrong. Following the ownership policy, it would result in a memory leak.

这是错误的。按照所有权策略,它将导致内存泄漏。

 – (NSArray *)sprockets {

    NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,
                               auxiliarySprocket, nil];
    return array;
}

The object’s reference to the new array object is limited to the sprockets method. After the method returns, the object loses its reference to the new object so cannot relinquish ownership. That in itself is not a problem. However, following the naming convention set out earlier, the caller is given no indication that it owns the returned object. The caller would therefore not relinquish ownership of the returned object, leading to a memory leak.

对象对新数组对象的引用仅限于spro佝偻病方法。方法返回后,对象失去对新对象的引用,因此不能放弃所有权。这本身不是问题。但是,按照前面的命名约定,调用方没有表示它拥有返回的对象。因此,调用方不会放弃返回的对象的所有权,导致内存泄漏。

#2


7  

Following up on @Chuck's comment, -convertDataToString must not return an object that the caller must release. That would violate the Three Magic Words. If you do not have "copy," "alloc," or "new" in your name, the caller cannot be expected to release the object. If you have "copy" in your name or start with "new" or "alloc," then the caller must release the object.

根据@Chuck的评论,-convertDataToString不能返回调用者必须释放的对象。这就违背了三个神奇的词。如果您的名字中没有“copy”、“alloc”或“new”,则不能期望调用者释放对象。如果您的名字中有“copy”,或者以“new”或“alloc”开头,那么调用者必须释放对象。

Objective-C relies heavily on consistent naming and the names mean things. If you learn the naming, then you won't have any problems.

Objective-C很大程度上依赖于一致的命名和名字的含义。如果你学会了命名,就不会有任何问题。

#3


2  

You'd want to return an autoreleased object most of the time. Unless your method name contains one of the following words [alloc, new, copy], you should return an autoreleased object.

您可能希望在大多数情况下返回一个自动上传的对象。除非您的方法名称包含下列单词之一[alloc, new, copy],您应该返回一个autorele对象。

#4


1  

If you create, alloc, or copy an object, you are responsible for releasing it. Based on this, you should return an autoreleased object.

如果您创建、alloc或复制一个对象,您将负责释放它。基于此,您应该返回一个自动上传的对象。

#5


0  

I prefer to return the autorelease. It means that you aren't hunting around trying to find where memory is being freed. Keeping memory allocation and deallocation together makes your life easier. After all, you're coding this, why make it harder on yourself.

我更愿意退货。这意味着你并不是在四处搜寻,试图找到释放记忆的地方。保持内存分配和重新分配,使您的生活更轻松。毕竟,你在编码这个,为什么让它对你自己更困难。

#6


0  

Both are acceptable, but you should name your method accordingly : if the caller has the responsibility to deallocate it, you have to make this explicit by having your method name contain "create", "alloc" or "copy", else it should not. More reading on this at http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH

两者都是可以接受的,但是您应该相应地命名您的方法:如果调用者有责任释放它,那么您必须让您的方法名包含“create”、“alloc”或“copy”,以使其显式,否则它不应该。在http://developer.apple.com/library/mac/#文档/cocoa/概念性/MemoryMgmt/ article /mmRules.html%23/ apple_ref/doc/uid/20000994-BAJHFBGH上有更多阅读

It might be a bit more customary to return an autorelease object, but both are okay.

返回一个autorelease对象可能有点习惯,但两者都可以。