Ios “';NSUnknownKeyException';,原因:…;此类不符合key scoreArray”;的键值编码;
我正在尝试此代码进行排序,但我遇到了崩溃Ios “';NSUnknownKeyException';,原因:…;此类不符合key scoreArray”;的键值编码;,ios,objective-c,iphone,Ios,Objective C,Iphone,我正在尝试此代码进行排序,但我遇到了崩溃 NSMutableArray * scoreArray = [[[[returnedArr objectAtIndex:0]valueForKey:@"Questionnaire_Answers__r"] valueForKey:@"records"] valueForKey:@"Score__c"]; dispatch_async(dispatch_get_main_queue(), ^{ answerArray = [[[
NSMutableArray * scoreArray = [[[[returnedArr objectAtIndex:0]valueForKey:@"Questionnaire_Answers__r"] valueForKey:@"records"] valueForKey:@"Score__c"];
dispatch_async(dispatch_get_main_queue(), ^{
answerArray = [[[NSSet setWithArray:answerArray] allObjects] mutableCopy];
NSArray *filteredArray = [NSArray arrayWithArray:answerArray];
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scoreArray" ascending:NO]; // Describe the Key value using which you want to sort.
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
answerArray = [[filteredArray sortedArrayUsingDescriptors:descriptors] mutableCopy]; // Now Sort the Array using descriptor.
[self.tblview reloadData];
});
initWithKey:表示模型的变量,而不是数组的名称。显示您的scoreArray数据。@Mahesh我的scoreArray数据是3、1、2、4、0似乎您不了解排序描述符的工作原理:键不是数组的变量名,而是字典的键或数组中自定义类的属性。除非您知道KVC方法的作用,否则不要使用
valueForKey:
。首先调试代码并找到您的代码崩溃的地方?@VerveIOS由于initwithkey@“scoreArray”而崩溃
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scoreArray" ascending:NO];