简体   繁体   English

为什么在尝试将addObject添加到NSMutableArray时会收到SIGABRT

[英]Why do I get SIGABRT when trying to addObject to NSMutableArray

I have two classes - BNRItem and BNRContainer. 我有两个类--BNRItem和BNRContainer。 BNRContainer is a subclass of BNRItem. BNRContainer是BNRItem的子类。 In order to cut down the amount of code I paste, assume the following that I have tested and know works: 为了减少我粘贴的代码量,假设我已经测试并知道以下工作:

+(BNRItem * ) randomItem; // allocate and init a random item.

@property(nonatomic, readwrite, copy) NSMutableArray * subitems; // This is a property of BNRContainer class

main.m:

NSMutableArray * rand_items = [NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
    [rand_items addObject: [BNRItem randomItem]];
}

[rand_items addObject: @"HELLO"];

BNRContainer * rand_container_of_items = [BNRContainer randomItem];
rand_container_of_items.subitems = rand_items;

[rand_container_of_items.subitems addObject: @"THERE"]; // ERROR SIGABRT

NSLog(@"------------------------------------------------------");
NSLog(@"%@", rand_container_of_items);

rand_container_of_items = nil;

If I NSLog without adding @"THERE", I see "HELLO" in my description so I know that I am able to call addObject: at that point. 如果我没有添加@“THERE”的NSLog ,我在我的描述中看到“HELLO”所以我知道我能够在那时调用addObject: Why do I get SIGABRT when I am trying to access ivar "subitems" of rand_container_of_items? 当我尝试访问rand_container_of_items的ivar“subitems”时,为什么我会收到SIGABRT? I just can't figure this one out. 我只是想不出这个。

The problem seems to be the copy modifier in your declaration. 问题似乎是声明中的复制修饰符。

@property (nonatomic, readwrite, copy) NSMutableArray *subitems;

In the documentation , the NSCopying protocol conformance is inherited form NSArray, so my suspicion is that in this line 文档中 ,NSCopying协议一致性是从NSArray继承的,所以我怀疑是在这一行

rand_container_of_items.subitems = rand_items;

subitems contains an immutable copy of the original array. subitems包含原始数组的不可变副本。 Try removing copy from your declaration. 尝试从声明中删除副本 If you need a copy use the mutableCopy method. 如果需要副本,请使用mutableCopy方法。

Problem lies here 问题出在这里

property(nonatomic, readwrite, copy) NSMutableArray * subitems;

You should not use copy here since it will return immutable copy of the object. 你不应该在这里使用copy ,因为它将返回对象的immutable副本。 So that you cannot add objects to it. 这样你就无法向它添加对象。 It could be 它可能是

property(nonatomic, strong) NSMutableArray * subitems;

This line is giving sigbart as when you allocate an array to mutable array, it becomes mutable. 当你将一个数组分配给可变数组时,这一行给出了sigbart,它变得可变。

So, when you are copying rand_items to rand_container_of_items.subitem, it is becoming mutable. 因此,当您将rand_items复制到rand_container_of_items.subitem时,它变得可变。

So, to make it immutable, try following : 因此,要使其不可变,请尝试以下操作:

BNRContainer * rand_container_of_items = [BNRContainer randomItem];
rand_container_of_items.subitems = [rand_items mutablecopy];

[rand_container_of_items.subitems addObject:@"THERE"]; // ERROR SIGABRT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM