【问题标题】:Adding objects to NSMutableArray properties crashes with unrecognized selector?将对象添加到 NSMutableArray 属性会因无法识别的选择器而崩溃?
【发布时间】:2012-07-17 13:52:00
【问题描述】:

当我尝试将对象添加到 NSMutableArray 时,我在这里收到“-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0”。我不知道为什么。

我有一组嵌套的自定义类。 WTEvent 有一个名为 mats 的 NSMutableArray 属性,它由 WTMat 对象组成。 mats 有一个称为 bouts 的 WTBout 对象的 NSMutableArray。

我希望能够在比赛中更新一场比赛,而让其余比赛保持不变。在这里,我找到了需要分配回合的垫子。如果它没有初始化,我初始化它。然后,如果它是空的,我尝试向它添加一个 newBout,它会崩溃。

难道我不能像这样更改 NSMutableArray 的元素吗?

// Locate the correct mat
WTMat* mat = [self getMatFromBoutKey:[updateData valueForKey:@"boutKey"]];
WTBout* newBout = [WTBout buildBoutFromDictionary:updateData];

// need to initialize bouts
if ([mat bouts] == nil) {
    NSLog(@"Initializing bouts");
    NSMutableArray* newBouts = [[NSMutableArray alloc] init ];
    [mat setBouts:newBouts];
}

if ([[mat bouts] count]) {
    // if bouts has bouts, then adjust the bout stack

    NSLog(@"bouts is not empty");
} else {
    // if bouts is empty, just add a bout

    NSLog(@"Adding a newBout");
    NSMutableArray* oldBouts = [mat bouts];
    NSLog(@"oldbouts: %@", [oldBouts description]);
    [oldBouts addObject:newBout]; // -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0

}

WTMat.h:

#import <Foundation/Foundation.h>

@interface WTMat : NSObject
{
    NSString* matName;
    NSString* boutKey;
    NSString* multicastAddress;
    NSMutableArray* bouts;
}

@property (strong, nonatomic) NSString* matName;
@property (strong, nonatomic) NSString* boutKey;
@property (strong, nonatomic) NSString* multicastAddress;
@property (copy, nonatomic) NSMutableArray* bouts; 

@end

日志:

2012-05-12 08:14:00.491 Wrestling Tools[12623:403] Initializing bouts
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] Adding a newBout
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] oldbouts: (
)
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0
2012-05-12 08:14:00.494 Wrestling Tools[12623:403] (
    0   CoreFoundation                      0x00007fff96783fc6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff92375d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff968102ae -[NSObject doesNotRecognizeSelector:] + 190
    3   CoreFoundation                      0x00007fff96770e73 ___forwarding___ + 371
    4   CoreFoundation                      0x00007fff96770c88 _CF_forwarding_prep_0 + 232
    5   Wrestling Tools                     0x000000010dc49343 -[WTUpdater fullUpdate:] + 835

【问题讨论】:

  • 在调试器中输入 po 0x10dd15ee0 以查看哪个对象被发送到错误的选择器。
  • 我看到问题是我将 bouts 属性设置为“复制”,这给了我一个不可变的副本,它不知道 addObject 方法:我已将其改回强,例如我的其他属性,但我不确定这会对我的应用程序造成什么样的破坏。如何管理从其他类的多个实例访问的可变数组。这会是一场噩梦吗?

标签: objective-c properties nsmutablearray unrecognized-selector


【解决方案1】:

这是你的问题:

@property (copy, nonatomic) NSMutableArray* bouts; 

“copy”参数生成数组的不可变副本,这意味着当此行运行时:

NSMutableArray* oldBouts = [mat bouts];

你实际上得到了一个普通的NSArray,这就是你得到无法识别的选择器错误的原因。

解决此问题的简单方法是将“复制”关键字更改为“强”。稍微困难的是,将其保留为副本,而是将属性的类型更改为 NSArray,尽管这会使修改数组变得复杂。

最后,您可以自己实现setBouts:,它会使用-mutableCopy 方法进行复制,以确保它制作的副本是可变的。

【讨论】:

    【解决方案2】:

    我认为这完全取决于您的NSMutableArray* oldBouts = [mat bouts]; 分配它,看看会发生什么。

    【讨论】:

      【解决方案3】:

      尝试使用@dynamic 初始化属性。 very similar answer 可能会对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2015-01-08
        • 1970-01-01
        • 2012-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-24
        相关资源
        最近更新 更多