NSMutableArray 中的所有元素复制后消失
我正在使用 NSMutableArray 初始化视图控制器。但在 init 中,我必须使用 NSMutableArray 进行 init,但此后所有元素都消失了。
UIViewController* vc = [[[PhotoAlbumViewController alloc] initWithImages:imageArrayBig] autorelease];
下面是上面函数的定义。
// This is the definition of the init function
- (id)initWithImages:(NSMutableArray *)bigImages {
if ((self = [self initWithNibName:nil bundle:nil])) {
_imageArrayBig = [[NSMutableArray alloc] init];
for (ArrayObject* tmp in bigImages) {
[_imageArrayBig addObject: tmp];
}
}
// After the copying, all the elements in bigImages and _imageArrayBig disappear :(
return self;
}
有什么想法吗?是否存在内存泄漏?
I am initializing a view controller with a NSMutableArray. But in init, I have to init with NSMutableArray, but all the elements disappeared after that.
UIViewController* vc = [[[PhotoAlbumViewController alloc] initWithImages:imageArrayBig] autorelease];
Below is the definition of the function above.
// This is the definition of the init function
- (id)initWithImages:(NSMutableArray *)bigImages {
if ((self = [self initWithNibName:nil bundle:nil])) {
_imageArrayBig = [[NSMutableArray alloc] init];
for (ArrayObject* tmp in bigImages) {
[_imageArrayBig addObject: tmp];
}
}
// After the copying, all the elements in bigImages and _imageArrayBig disappear :(
return self;
}
Any idea? Is there a memory leak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您同时使用
imageArrayBig
和_imageArrayBig
,因此您似乎将属性名称与实例名称混合在一起。最好坚持使用一个,最好是
self.imageArrayBig
这是属性您还可以使用
addObjectsFromArray
更有效地复制数组,例如
It looks like you are mixing the property name with the instance name since you use both
imageArrayBig
and_imageArrayBig
.It is better to stick with one, preferably
self.imageArrayBig
which is the propertyYou can also copy the array more effectively by using
addObjectsFromArray
something like
您发布的代码很好,但可以按照 @anders 的建议进行优化。你不需要财产。当您传递给 init 时,您确定 bigImages 中有对象吗?添加 NSLog 来验证:
也不清楚为什么需要传入可变数组。如果您要创建一个新的可变数组,您可以传入一个
NSArray
。The code you posted is fine but can be optimized as @anders suggests. You don't need a property. Are you sure that
bigImages
has objects in it when you pass to init? Add an NSLog to verify:It's also not clear why you need to pass in a mutable array. If you are creating a new mutable array you can pass in an
NSArray
.