可变数组内部的自定义类对象的内存处理 [英] Memory Handling for Object of Custom Class Which is inside Mutable Array
问题描述
我搜索了这个问题,但找不到任何合适的答案:
我已经在 java
上工作了很长时间了,现在我想学习 Objective-C
I searched about this question but can't found any suitable answer:
I had been working on java
for a long time now i want to learn Objective-C
这里是场景:
我有一个 Objective C
类,扩展了 NSObject
类,该类包含 NSString
类型和<$ c我创建了$ c> CustomObject 类型,其中的属性是 @property(nonatomic,retain)NSString或MyObjec
t。
创建自定义类的对象后,将其作为 [myArray addObject:myObject]
Here is the scenario:
I have an Objective C
class which Extends NSObject
class that class contains saveral fields of NSString
type and CustomObject
type i created there property with @property(nonatomic,retain) NSString or MyObjec
t.
After creating object of my custom class i put it in NSArray as [myArray addObject:myObject]
现在我要问是否要释放myArray,我必须释放该数组内的对象以及该对象内的字段,而某些字段也太过 CustomObject
属于另一个类了吗?
now what i want to ask if i release myArray do i have to release objects inside that array and fields inside that object and some fields are too CustomObject
of another class;
推荐答案
取消分配NSArray时,会向该数组中的每个对象发送释放消息。如果对对象的唯一引用是在数组中,则对象将调用其dealloc方法,该方法负责释放其所有属性。
When you deallocate an NSArray a release message is sent to every object in the array. If the only reference to your object is in the array, then your object will call its dealloc method, which is in charge of releasing all of its properties.
这篇关于可变数组内部的自定义类对象的内存处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!