gpt4 book ai didi

objective-c - 释放使用 initWithInteger : vs. numberWithInt 创建的 NSNumber 实例:

转载 作者:行者123 更新时间:2023-12-03 16:50:13 25 4
gpt4 key购买 nike

在《Programming in Objective-C 2.0》一书中的第 326 页上,作者说道:

myNumber = [[NSNumber alloc] initWithInt: 1000];  

Of course, based on previous discussions, if you create myNumber this way, you are re- sponsible for subsequently releasing it when you’re done using it with a statement such as follows:

[myNumber release];  

我的问题是:

这是否意味着如果我使用此语句创建一个 NSNumber 对象

NSNumber *myNumber = [NSNumber numberWithInteger: 100];  

我不必自己释放对象 myNumber 吗?

最佳答案

This link is your bible

对于[NSNumber numberWithInt:],它返回一个自动释放的对象,您不需要执行任何操作即可正确释放它。当然,除非您保留它,在这种情况下,您可能会从您的dealloc方法调用它的release

[[NSNumber alloc] initWithInt:] 返回一个保留计数为 1 的对象(通过调用 alloc)。您负责释放以此方式创建的任何对象。

关于objective-c - 释放使用 initWithInteger : vs. numberWithInt 创建的 NSNumber 实例:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com