Objective - C中无法识别的选择发送到实例
我收到此错误:Objective - C中无法识别的选择发送到实例
'-[ProductionCellData objectForKey:]: unrecognized selector sent to instance 0x14dca5d0'
在这行代码:
NSString *productionItem = [[myArray objectAtIndex:keyCounter] objectForKey:@"baseLineStart"];
keyCounter等于0,这里是myArray的样子
我试图让baseLineStart的价值,但我磕ep得到上面的错误....我该如何解决这个问题?
objectForKey
通常用于词典。
如果您要访问的属性,只是引用它:
NSString *productionItem = [myArray objectAtIndex:keyCounter].baseLineStart;
或者用更现代的语法:
NSString *productionItem = myArray[keyCounter].baseLineStart;
你是代码给了我一个错误'属性'baseLineStart'找不到'id''类型的对象 – user979331
您必须将其转换,例如'NSString * string =((ProductionCellData *)myArray [keyCounter]).productionItem;' – chr
事实是,ProductionCellData做笔记响应此选择,您应该使用
- (nullable id)valueForKey:(NSString *)key;
或以这种方式从对象获取属性(如果它是公开的)
NSString *productionItem = [[myArray objectAtIndex:keyCounter] baseLineStart];
您的代码给了我这个错误'没有已知的实例方法选择'baseLineStart'' – user979331
尝试通过以下方式查找错误。
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
它看起来像'ProductionCellData'是一个自定义的类而不是'NSDictionary','baseLineStart'是一个属性。 – vadian