2014-12-31 74 views
-1

我聲明瞭在firstviewcontroller陣列等添加對象,具有自定義委託功能給錯誤

@interface ClassesList : UIViewController 
{ 
    NSMutableArray *className; 
} 
@property (retain,nonatomic) NSMutableArray *className; 

並限定委託方法在下一視圖控制器對象添加到數組的NSMutableArray

@protocol Createclass <NSObject> 

-(void)addnewObject:(NSString*)string;; 

@end 

併發送字符串與文本文件的字符串

[self.delegate addnewClass:_className.text]; 

while calling this met OD在firstviewcontroller它顯示錯誤

NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector  sent to instance 

這裏是我的編輯

-(void)addnewClass:(NSString *)string 
{ 
[className addObject:string]; 
[self.table reloadData]; 
} 

我越來越從plist文件存儲類

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"classesArray.plist"]; 


NSDictionary *StudentDict=[[NSDictionary alloc]initWithContentsOfFile:path]; 



NSArray *See = StudentDict[@"alldata"]; 

className = [[NSMutableArray alloc]init]; 

className= [See valueForKey:@"class_name"]; 
+0

您能否在您的firstViewController中發佈 - (void)addnewObject:(NSString *)字符串的代碼 – AntonijoDev

+0

Adn yout協議方法被稱爲addnewObject而不是addnewClass,所以它應該是[self.delegate addnewObject:_className。文本]; – AntonijoDev

+0

@AntonijoDev對不起addnewObject只是爲發佈問題而改變 – user3595019

回答

0

如果[見valueForKey:@ 「CLASS_NAME」]包含數組,那麼這是你的錯誤原因:

[See valueForKey:@"class_name"]; 

收益不變的陣列。

替換:

className = [[NSMutableArray alloc]init]; 
className= [See valueForKey:@"class_name"]; 

有:

className= [NSMutableArray arrayWithArray:[See valueForKey:@"class_name"]]; 

,你應該罰款。

+0

tnx u救了我很多 – user3595019

+0

沒有pob,當你賺足夠的代表,請接受這個答案,thx – AntonijoDev

2

錯誤信息列表的字典你看到:

reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 

意味着你有一個不可改變的數組,並且你試圖對它進行改變(調用addObject:),並且它沒有實現該方法。

您還沒有表現出所有的委託執行的,但是這將表明它工作正常,而這個問題是你已經設置className不可變的,而不是一個可變的數組。


這裏:

className = [[NSMutableArray alloc]init]; 

className= [See valueForKey:@"class_name"]; 

您創建一個空的可變數組,然後用一個不變的陣列(加載像總是返回不可變對象)替換它。

您應該刪除第一行並更改第二行以獲得mutableCopy或使用屬性列表序列化API,該API允許您創建可變集裝箱和樹葉。

+0

看看我聲明的className – user3595019

+0

聲明並不意味着這是它指向如果你犯了一個錯誤。它可能是一個指向NSNumber的指針,它不會導致問題,直到你嘗試使用它作爲一個可變數組... – Wain

+0

所以如何添加到我的類名數組任何建議 – user3595019