NSInvalidArgumentException - 无法识别的选择器发送到实例
Posted
技术标签:
【中文标题】NSInvalidArgumentException - 无法识别的选择器发送到实例【英文标题】:NSInvalidArgumentException - unrecognized selector sent to instance 【发布时间】:2013-10-22 20:39:17 【问题描述】:我正在使用以下函数使用 SearchDisplayController 进行搜索
- (void)handleSearchForTerm:(NSString *)searchTerm
[self.searchResults removeAllObjects]; // First clear the filtered array.
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
FMResultSet *results = [db executeQuery: [NSString stringWithFormat:@"SELECT * FROM periods where (searchstartyear <= %@) AND (searchendyear >= %@)", searchTerm, searchTerm]];
NSMutableArray *array = [[NSMutableArray alloc] init ];
[array removeAllObjects];
while ([results next])
Periods *searchPeriod = [[Periods alloc] init];
searchPeriod.periodname = [results stringForColumn:@"PeriodName"];
searchPeriod.startyear = [results stringForColumn:@"StartYear"];
searchPeriod.endyear = [results stringForColumn:@"EndYear"];
searchPeriod.thumb = [results stringForColumn:@"Thumb"];
searchPeriod.childid = [results stringForColumn:@"ChildID"];
[array addObject:searchPeriod];
[self.searchResults addObject:array];
[db close];
期间是一个对象
#import <UIKit/UIKit.h>
@interface Periods : NSObject
@property (nonatomic,strong) NSString *periodname;
@property (nonatomic,strong) NSString *startyear;
@property (nonatomic,strong) NSString *endyear;
@property (nonatomic,strong) NSString *thumb;
@property (nonatomic,strong) NSString *childid;
@end
当执行以下代码时,我得到了 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"PeriodsCell";
UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Configure the cell...
if (tableView != [[self searchDisplayController] searchResultsTableView])
NSMutableArray *array = [periodsdataarray objectAtIndex:indexPath.section];
NSMutableDictionary *dictionary = [array objectAtIndex:indexPath.row];
UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
childIDLabel.text = [dictionary valueForKey:@"ChildID"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
titleLabel.text = [dictionary valueForKey:@"PeriodName"];
UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
previewLabel.text = [NSString stringWithFormat:@"%@ - %@",[dictionary valueForKey:@"StartYear"],[dictionary valueForKey:@"EndYear"]];
UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
[imageview setImage:[UIImage imageNamed:[dictionary valueForKey:@"Thumb"]]];
else
Periods *periodcell = [self.searchResults objectAtIndex:indexPath.row];
cell.tag = 666;
UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
childIDLabel.text = periodcell.childid;
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
titleLabel.text = periodcell.periodname;
UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
previewLabel.text = [NSString stringWithFormat:@"%@ - %@",periodcell.startyear,periodcell.endyear];
UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
[imageview setImage:[UIImage imageNamed:periodcell.thumb]];
return cell;
...更具体地说,当执行此行时:
childIDLabel.text = periodcell.childid;
我做错了什么?
【问题讨论】:
选择器发送到的实例的类是什么? 我很抱歉,但我不确定我是否理解您的问题.. 你提到你得到了一个例外。通常异常会提到类名。 感谢您回来。这是异常消息的全文: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM childid]: unrecognized selector sent to instance 0xe2b5a70' 感谢您的宝贵时间。我能够找到我的错误......在我的搜索功能中,我将一个数组添加到我的 searchResults 数组而不是我的 Periods 对象, 【参考方案1】:我犯的错误是在 handleSearchForTerm 函数中。
我使用 NSMutableArray *array 来保存 Periods 对象的值,然后将该数组添加到我的 searchResults 数组中(期望 searchResults 保存 Periods)。解决方案很简单,只需将句点直接添加到我的 searchResults 数组并完全放弃数组......
【讨论】:
以上是关于NSInvalidArgumentException - 无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章