2013-08-06 96 views
3

刪除對象時,我有,我已經在初始化一個viewDidLoadNSMutableArray崩潰從NSMutableArray的

在.H
self.titlesTagArreys = [@[@"Dollar", @"Euro", @"Pound",@"Dollar longString", @"Euro longStringlongString", @"Pound",@"Dollar", @"Euro", @"PoundlongStringlongString"]mutableCopy]; 

@property(nonatomic, copy) NSMutableArray* titlesTagArreys; 

當我試圖刪除一個項目,應用程序崩潰:

-(void)removeButtonWasPressed:(NSString*)tagTitle{ 
    NSLog(@"tagTitle - %@",tagTitle); 
    NSLog(@"self.titlesTagArreys - %@",self.titlesTagArreys);  
    [self.titlesTagArreys removeObject:tagTitle]; 
} 

這裏是日誌:

2013-08-06 16:15:03.989 EpicTv[6378:907] tagTitle - Dollar 
2013-08-06 16:15:03.991 EpicTv[6378:907] self.titlesTagArreys - (
    Dollar, 
    Euro, 
    Pound, 
    "Dollar longString", 
    "Euro longStringlongString", 
    Pound, 
    Dollar, 
    Euro, 
    PoundlongStringlongString 
) 
[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0' 
*** First throw call stack: 
(0x327162a3 0x3a5c197f 0x32719e07 0x32718531 0x3266ff68 0x20ad55 0x20c9a5 0x20bf5d 0x346090c5 0x34609077 0x34609055 0x3460890b 0x34608e01 0x345315f1 0x3451e801 0x3451e11b 0x362295a3 0x362291d3 0x326eb173 0x326eb117 0x326e9f99 0x3265cebd 0x3265cd49 0x362282eb 0x34572301 0xafb89 0xa4d68) 
libc++abi.dylib: terminate called throwing an exception 
+0

可以顯示初始化'NSMutableArray'的代碼,因爲'removeObject'是'NSMutableArray'的有效函數,但錯誤'無法識別的選擇器...'表示'titlesTagArreys'不是'NSMutableArray'。請分享使用'titlesTagArreys'的代碼。 – Popeye

+0

@Luda如何聲明NSMutableArray(titlesTagArreys)? – DharaParekh

+0

Hakuna'NSMutata'。 – dreamlax

回答

6

看來titlesTagArray的列表不是NSMutableArray,因爲removeObject不能被調用。 也許你在代碼前面通過NSArraytitlesTagArreys

嘗試init您的磁盤陣列和

self.titlesTagArreys = [NSMutableArray arrayWithArray:@[@"...",@"...",...]]; 

@屬性(非原子,副本),使你NSMutableArray的一個不可變的副本。嘗試@屬性(非原子,保留),而不是拷貝

5

我也認爲你titlesTagArreys不是可變數組因爲一些代碼更改

嘗試添加:NSLog(@"%@", NSStringFromClass(self.titlesTagArreys.class));檢查你用什麼類

-(void)removeButtonWasPressed:(NSString*)tagTitle{ 

    NSLog(@"%@", NSStringFromClass(self.titlesTagArreys.class)); 

    [self.titlesTagArreys removeObject:tagTitle]; 
} 
0

我有同樣的問題,並得知您必須覆蓋設置器爲可變數組屬性和調用mutableCopy。你可以在Stack Overflow here找到答案。

+1

只有在使用'copy'定義屬性時纔是如此。如果它是用'strong'(而不是'copy')定義的,那麼就不需要重寫setter方法。 – rmaddy

0

titlesTagArray不是NSMutableArray。這是因爲在日誌中我們可以看到[__NSArrayI removeObject:]無法識別的選擇器發送到實例0x1c53bbd0。 另外,NSArrayI用於NSArray,NSArrayM用於NSMutableArray。 你必須用NSArray初始化可變數組,因此是個例外。