从方法返回 NSString 时的内存管理和所有权

发布于 2024-10-11 08:21:07 字数 511 浏览 0 评论 0原文

我正在 Objective-C 中编写一个方法,该方法根据 NSMutableArray 输入返回一个字符串。我不打算让调用者修改字符串,但也不在乎。该方法当前的签名如下:

- (NSMutableString *) generateString:(NSMutableArray *)myArray

该项目目前是一个命令行基础工具,旨在了解诀窍,但这最终将出现在 iPhone 应用程序中。

我的问题是:

  1. 返回 NSString 会更谨慎吗?

2)返回 NSMutableString 时我应该使用 autorelease 吗?如果您建议我返回一个 NSString ,那么如何修改返回它?

3)如果我还想返回一个错误对象来标识是否发生错误,也许是一个枚举和一个字符串,在这种情况下所有权如何工作?该方法不称为 alloc、new 或 copy,那么调用者如何知道呢?除了上述方法中的字符串之外,我如何返回分配的错误对象?

谢谢!

I'm writing a method in Objective-C which is to return a string based on an NSMutableArray input. I don't intend for the callers to modify the string but couldn't care less either. The method's current signature reads as:

- (NSMutableString *) generateString:(NSMutableArray *)myArray

The project is currently a command-line Foundation Tool in order to learn the ropes but this will ultimately end up in an iPhone application.

My questions are:

  1. Would it be more prudent to return an NSString?

2) When returning the NSMutableString should I use autorelease? If you're recommending I return an NSString how does that modify returning it?

3) If I also want to return an error object identifying if an error occurred, perhaps an enum and a string how does ownership work in that case? The method isn't called alloc, new or copy so how would the caller know? How would I return the allocated error object in addition to the string in the above method?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇到 2024-10-18 08:21:07
  1. 在大多数情况下,我会返回一个 NSString 。它的内存占用稍小,如果调用者需要一个可变副本,只需一个方法即可创建一个:mutableCopy
  2. 您不仅应该而且必须自动释放您的方法返回的NSStringNSMutableString(至少如果您像示例中那样命名您的方法)! 内存管理编程指南列出了您必须遵循的规则,以避免完全混乱,并深入解释了所有内容。所以读吧!
  3. Foundation 框架使用 NSError 类来描述错误。要“返回”附加参数,您的方法将如下所示:

    - (NSString *)generateString:(NSMutableArray *)myArray 错误:(NSError **)error
    {
        BOOL 失败;
        // 做一些花哨的事情,如果操作失败,failed 为 true
        如果(失败==是){
            如果(错误!= NULL){
                *错误= [NSError errorWithDomain:@“your_domain”代码:123 userInfo:nil];
            }
            返回零;
        }
    
        返回[yourString自动释放];
    }
    
    // ...
    
    // 调用你的方法
    NSError *错误= nil;
    [someObject生成字符串:someArray错误:&错误];
    

请注意两个 ** ,并且任何 NSError var 可能是 NULL 而不是 < code>nil (因此您应该在方法的主体中检查这一点)。

  1. I would return a NSString in most cases. It has a slightly smaller memory footprint and if the caller needs a mutable copy, creating one is just a method away: mutableCopy.
  2. You not only should but have to autorelease the NSString or NSMutableString your method returns (at least if you name your method like in the example)! The Memory Management Programming Guide lists rules you must follow to avoid a complete mess and explains everything in-depth. So read it!
  3. The Foundation framework uses the NSError class to describe errors. To "return" additional parameters your method would look like this:

    - (NSString *)generateString:(NSMutableArray *)myArray error:(NSError **)error
    {
        BOOL failed;
        // Do some fancy stuff, if the operation fails, failed is true
        if (failed == YES) {
            if (error != NULL) {
                *error = [NSError errorWithDomain:@"your_domain" code:123 userInfo:nil];
            }
            return nil;
        }
    
        return [yourString autorelease];
    }
    
    // ...
    
    // Calling your method
    NSError *error = nil;
    [someObject generateString:someArray error:&error];
    

Note the two ** and that any NSError var may be NULL and not nil (so you should check for this in the method's body).

柳絮泡泡 2024-10-18 08:21:07

默认情况下,方法返回 autorelease NSString,我建议使用相同的行为。如果需要修改结果字符串,可以使用临时字符串。

By default methods return autorelease NSString, and I'd recomment to use the same behaviour. If you will need to modify resulting string, you can use temporary strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文