惯用的短寿命本地对象类似于 RAII

发布于 2024-12-09 15:25:50 字数 719 浏览 1 评论 0原文

我遇到了 Objective-C 的这个片段:

NSNumber *theBalance = 
    [[[NSNumberFormatter alloc] init]
     numberFromString: [textField text]];

这似乎泄漏了 NSNumberFormatter。在 C++ 中,我会做以下两件事之一:

  1. 使用 auto (即堆栈)存储 NSNumberFormatter
  2. 使用 RAII(例如 shared_ptr)来管理NSNumberFormatter 的生命周期

在 Objective-C 中,这两个选项似乎都不可能。我在堆栈上尝试过:

NSNumberFormatter fmt;

但这无法编译。据我所知,Objective-C 中没有直接相当于 RAII 的东西。作为一个主要是 C++ 的程序员,我可能从错误的角度看待这个问题,所以:

  1. 在一般情况下,处理像 NSNumberFormatter 这样的对象的生命周期的正确的、惯用的(现代的)Objective-C 方式是什么?代码>这里?我真的必须自己明确地做吗?
  2. 在具体案例中是否有更好的方法来解决实际问题?

I came across this fragment of Objective-C:

NSNumber *theBalance = 
    [[[NSNumberFormatter alloc] init]
     numberFromString: [textField text]];

This seems to leak the NSNumberFormatter. In C++ I would do one of two things:

  1. use auto (i.e. stack) storage for the NSNumberFormatter
  2. use RAII (e.g. shared_ptr) to manage the life of the NSNumberFormatter

In Objective-C neither of these options seem to be possible. I tried on the stack:

NSNumberFormatter fmt;

But this doesn't compile. As far as I can find there's no direct equivalent of RAII in Objective-C. I'm probably looking at the problem from the wrong angle as a mainly C++ programmer, so:

  1. In the general case what's the correct, idiomatic (modern) Objective-C way of handling the life of objects like the NSNumberFormatter here? Do I really have to do it explicitly myself?
  2. In the specific case is there a better way of solving the actual problem?

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

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

发布评论

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

评论(1

空名 2024-12-16 15:25:50

大多数类(例如 NSStringNSArray 等)都具有方便的构造函数,例如 [NSString string][NSArray array] 返回自动释放的对象。 NSNumberFormatter 没有任何便利的构造函数。因此,您可以发送一条 autorelease 消息,让它在自动释放池耗尽时自动释放。

NSNumber *theBalance = [[[[NSNumberFormatter alloc] init] autorelease]
                       numberFromString: [textField text]];

如果您想保留(拥有)对象的引用,您可以省略 autorelease 并在使用完毕后释放它。你就这样做吧,

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *theBalance = [numberFormatter numberFromString: [textField text]];

// Later... somewhere in your code...
[numberFormatter release];

我知道上面没有详细解释。我建议您阅读这篇文章,我希望您对内存管理有一些清晰的了解!

Most of the classes like NSString, NSArray, and so on, have the convenience constructors like, [NSString string] and [NSArray array] which return the autoreleased objects. NSNumberFormatter doesn't have any convenience constructors. So, you can send a autorelease message to let it autoreleased when the autorelease pool drains.

NSNumber *theBalance = [[[[NSNumberFormatter alloc] init] autorelease]
                       numberFromString: [textField text]];

If you want to retain(own) the reference of the object, you can omit the autorelease and release it later when you are done with it. You do it like this,

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *theBalance = [numberFormatter numberFromString: [textField text]];

// Later... somewhere in your code...
[numberFormatter release];

I know the above is not a detailed explanation. I'd suggest you to read this post by which, I hope, you would get some clear idea about memory management!

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