0

當我嘗試將對象添加到NSMutableArray時,我得到「 - [__ NSArrayI addObject:]:發送到實例0x10dd15ee0的無法識別的選擇器」。我不知道爲什麼。添加對象到NSMutableArray屬性崩潰與無法識別的選擇器?

我有一組嵌套的自定義類。 WTEvent有一個名爲mats的NSMutableArray屬性,它由WTMat對象組成。墊子有一個名爲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 
+0

在調試器中鍵入'po 0x10dd15ee0'來查看哪個對象正在發送錯誤的選擇器。 – fannheyward

+0

我看到的問題是我將bouts屬性設置爲'copy',這給了我一個不可變的副本,它不知道方法addObject:我已經將它改回爲強壯,就像我的其他屬性一樣,我不知道這會對我的應用程序造成什麼樣的破壞。我如何管理從其他類的多個實例訪問的可變數組。這會是一場噩夢嗎? – Adam

回答

5

這是你的問題:

@property (copy, nonatomic) NSMutableArray* bouts; 

「複製」參數使得陣列的不變副本,當這條線運行,這意味着:

NSMutableArray* oldBouts = [mat bouts]; 

你實際上得到一個普通的NSArray,這就是爲什麼你得到無法識別的選擇器錯誤。

這個簡單的解決方案是將「複製」關鍵字更改爲「強大」。稍微難一點的是將其作爲副本,而不是將屬性的類型更改爲NSArray,儘管這會使修改數組複雜化。

最後,你可以自己實現setBouts:,它使用-mutableCopy方法做副本,以確保它所做的副本是可變的。

0

我認爲這是所有關於你NSMutableArray* oldBouts = [mat bouts];的Alloc它,看看會發生什麼。

相關問題