NSMutableArray 中的所有元素复制后消失

发布于 2024-12-22 00:41:21 字数 714 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2024-12-29 00:41:21

由于您同时使用 imageArrayBig_imageArrayBig,因此您似乎将属性名称与实例名称混合在一起。

最好坚持使用一个,最好是 self.imageArrayBig 这是属性

您还可以使用 addObjectsFromArray 更有效地复制数组,

例如

NSMutableArray* tmp = [[NSMutableArray alloc] init];
[tmp addObjectsFromArray:bigImages];
self.imageArrayBig = tmp;
[tmp release];

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 property

You can also copy the array more effectively by using addObjectsFromArray

something like

NSMutableArray* tmp = [[NSMutableArray alloc] init];
[tmp addObjectsFromArray:bigImages];
self.imageArrayBig = tmp;
[tmp release];
天赋异禀 2024-12-29 00:41:21

您发布的代码很好,但可以按照 @anders 的建议进行优化。你不需要财产。当您传递给 init 时,您确定 bigImages 中有对象吗?添加 NSLog 来验证:

- (id)initWithImages:(NSMutableArray *)bigImages {
    self = [self initWithNibName:nil bundle:nil];
    if (self) {
        NSLog(@"MyController:initWithImages: bigImages count = %d", [bigImages count]);
        _imageArrayBig = [[NSMutableArray alloc] initWithArray:bigImages];
    }
    return self;
}

也不清楚为什么需要传入可变数组。如果您要创建一个新的可变数组,您可以传入一个 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:

- (id)initWithImages:(NSMutableArray *)bigImages {
    self = [self initWithNibName:nil bundle:nil];
    if (self) {
        NSLog(@"MyController:initWithImages: bigImages count = %d", [bigImages count]);
        _imageArrayBig = [[NSMutableArray alloc] initWithArray:bigImages];
    }
    return self;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文