2015-10-13 89 views
-1

我收到此錯誤:Objective - C中無法識別的選擇發送到實例

'-[ProductionCellData objectForKey:]: unrecognized selector sent to instance 0x14dca5d0' 

在這行代碼:

NSString *productionItem = [[myArray objectAtIndex:keyCounter] objectForKey:@"baseLineStart"]; 

keyCounter等於0,這裏是myArray的樣子

enter image description here

我試圖讓baseLineStart的價值,但我磕ep得到上面的錯誤....我該如何解決這個問題?

+0

它看起來像'ProductionCellData'是一個自定義的類而不是'NSDictionary','baseLineStart'是一個屬性。 – vadian

回答

2

objectForKey通常用於詞典。

如果您要訪問的屬性,只是引用它:

NSString *productionItem = [myArray objectAtIndex:keyCounter].baseLineStart; 

或者用更現代的語法:

NSString *productionItem = myArray[keyCounter].baseLineStart; 
+0

你是代碼給了我一個錯誤'屬性'baseLineStart'找不到'id''類型的對象 – user979331

+0

您必須將其轉換,例如'NSString * string =((ProductionCellData *)myArray [keyCounter]).productionItem;' – chr

0

事實是,ProductionCellData做筆記響應此選擇,您應該使用

- (nullable id)valueForKey:(NSString *)key; 

或以這種方式從對象獲取屬性(如果它是公開的)

NSString *productionItem = [[myArray objectAtIndex:keyCounter] baseLineStart]; 
+0

您的代碼給了我這個錯誤'沒有已知的實例方法選擇'baseLineStart'' – user979331

0

嘗試通過以下方式查找錯誤。

NSDictionary *dic = [myArray objectAtIndex:keyCounter]; 
NSLog(@"dic:%@",dic); 
NSString *productionItem = [dic objectForKey:@"baseLineStart"]; 

//1. observe the dic object has "baseLineStart" key. if not found write the correct one. 
//2. objeserve what type of value returns "[myArray objectAtIndex:keyCounter]". it may not NSDictionary which may resoan of crash 
相關問題