在调用 TNSMutableDictionary.Create 之后我们需要调用 release 吗?

after calling TNSMutableDictionary.Create do we need to call release?

在阅读了 Dalija Prasnikar 的优秀 post 之后,我了解到:

Methods whose name begins with alloc, new, copy, or mutableCopy don't require calls to retain. On the contrary, if you call it you will create a memory leak because there will be one retain too many. But they do require release or autorelease. Those object instances are the ones you have created, and under Objective-C they are automatically retained when constructed.

但是创建呢?例如做 myDict: = TNSMutableDictionary.Create 以后我需要打电话给 myDict.release 吗?

两者的区别是什么

TNSMutableDictionary.Wrap(TNSMutableDictionary.Wrap(TNSMutableDictionary.OCClass.alloc).init);

TNSMutableDictionary.Create ?

简答:是的,如果你使用了Create(TNSMutableDictionary.Create)

,你需要调用release

对于对象的构建,使用哪种变体并不重要,但更短的变体总是更具可读性。在你的 Objective-C 版本中你有很多包装方式。


构造对象实例Delphi vs Objective-C

在Objective-C上构造对象的过程与 构造 Delphi 对象实例的过程。

虽然Delphi一次性构造对象,通过调用构造函数触发对象实例的内存分配,然后初始化对象,在Objective-C中该过程分为两步。一种是内存分配,用 alloc 方法,接下来是通过各种命名 init 完成的初始化 方法。这两个都是 Delphi 构造函数不同部分的近似等价物。

但这还不是全部。 Objective-C 也有 newallocinit 结合在一起,类似于 Delphi 构造函数所做的。但是,new 仅调用默认构造函数,如果您需要使用自定义构造函数,则需要使用 alloc + initxxx 变体。为了代码的一致性,Objective-C 开发人员通常更喜欢 alloc + init 即使他们可以使用 new.

Delphi Objective-C 个对象的包装器

Delphi 通过通用包装器 class 作为原始指针访问 Objective-C 对象。 class 提供了几个 class 方法来更容易实例化 Objective-C 对象。

Create 相当于调用 alloc.initnew,并调用默认值 初始化,而 Alloc 相当于调用 alloc,之后我们必须调用适当的初始化例程 - 一些 initxxx 或默认 init

Wrap 方法用于包装由 OS 提供的已构造对象实例并保持该对象实例存在,我们必须调用 retain,完成后 release

但是,如果您仅使用 Wrap 来包装 您创建的 具有 alloc.init 变体或 new 的对象实例,那么您不应该调用 retain,你必须调用 release.