我需要明确分配我的NSNumber吗? [英] Do I need to explicitly alloc my NSNumber?

查看:105
本文介绍了我需要明确分配我的NSNumber吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义一个数字,如下所示:

I am defining a number, as follows:

NSNumber *nn0 = [NSNumber numberWithInt:0];

没有任何分配,它可以正常工作。我的理解是,如果我使用numberWithInt,则会自动调用alloc和init。

It works fine without any alloc. My understanding is that if I use numberWithInt, alloc and init are called automatically.

如果我尝试在函数结束时释放,我会遇到问题:

If I try to release at the end of my function, I run into problems:

[nn0 release];

我收到运行时错误。

我的问题是:如果我使用numberWithInt初始化NSNumber,我是否必须对其进行任何内存管理?

My question is: if I use numberWithInt to initialise the NSNumber, do I have to do any memory management on it?

推荐答案

许多类型的便利构造函数产生一个自动自动释放的对象 - 即新对象将由当前的NSAutoreleasePool保留。您不需要手动释放这些对象 - 它们将在当前NSAutoreleasePool释放/耗尽时释放。

The "convenience constructors" for a lot of types produce an object that is automatically "autoreleased" - i.e. the new object will be retained by the current NSAutoreleasePool. You don't need to manually release these objects - they will be released when the current NSAutoreleasePool is released/drained.

有关便捷构造函数的说明,请参阅此页面,以及如何管理这些内存。

See this page for a description of convenience constructors, and how to mange the memory for these.

http://www.macdevcenter.com/pub/a/mac/2001/07/27/cocoa.html?page=3

这篇关于我需要明确分配我的NSNumber吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆