[__NSCFConstantString objectAtIndex:]:无法识别的选择器发送到实例 0x3141c

Posted

技术标签:

【中文标题】[__NSCFConstantString objectAtIndex:]:无法识别的选择器发送到实例 0x3141c【英文标题】:[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x3141c 【发布时间】:2012-11-30 14:53:21 【问题描述】:

我正在尝试让搜索显示控制器工作,但刚刚收到此错误:

2012-11-30 14:48:19.903 搜索故事[5812:c07] -[__NSCFConstantString objectAtIndex:]: 无法识别的选择器发送到实例 0x3141c 2012-11-30 14:48:19.904 searchstory[5812:c07] * 终止应用程序到期 未捕获的异常“NSInvalidArgumentException”,原因: '-[__NSCFConstantString objectAtIndex:]: 无法识别的选择器发送到 实例 0x3141c' * 第一次抛出调用栈:(0x1cc0012 0x10fde7e 0x1d4b4bd 0x1cafbbc 0x1caf94e 0x2901d 0xfff4b 0x10001f 0xe880b 0xe6ec2 0xf1d51 0xfa3ad 0x102454 0x329635 0x32c0de 0x27bd85 0x1111705 0x48873 0x10a2ce 0x109960 0x11678c 0x120bf6 0xbbedc9 0x1d1a0c5 0x1c74efa 0xaf3482 0x359509e 0x26e426a 0x2b1f78c 0x2b22588 0x356fd12 0x356fe4a 0x35705c0 0x35703b8 0x356f904 0x295eeab 0x295ee13 0x3542596 0x356ebcc 0x2b1cac6 0x2b1bd56 0x2b4f320 0x31a9afd 0x2b45e66 0x2b470aa 0x2b4501e 0x31a81aa 0x2b5f491 0x2b4f0ca 0x2b2303e 0x27207fb 0x11116b0 0x358aed0 0x275f384 0x27614df 0x206254 0x1c904f 0x1ca3b8 0x1c8f5e 0x1d312d 0x5643b 0x153e05 0x153e05 0x153e05 0x153e05 0x153e05 0x153e05 0x153e05 0x153e05 0x153e05 0x56495 0x55e66 0x5624b 0x47cf8 0x1c1bdf9 0x1c1bad0 0x1c35bf5 0x1c35962 0x1c66bb6 0x1c65f44 0x1c65e1b 0x1c1a7e3 0x1c1a668 0x4565c 0x1c8d 0x1bb5) libc++abi.dylib: 终止调用抛出一个 异常

我一直在玩弄代码,试图让它工作。我用于搜索和 tablerow 的代码是:

    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath




    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = [[[[self searchResults]objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
    else
        contentForThisRow = [[[[self contentsList] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

    static NSString *CellIdentifier = @"stoneCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    
        cell.textLabel.text = [[[self.contentsList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        // Do anything that should be the same on EACH cell here.  Fonts, colors, etc.
    

    // Do anything that COULD be different on each cell here.  Text, images, etc.
    [[cell textLabel] setText:contentForThisRow];


    return cell;


- (void)handleSearchForTerm:(NSString *)searchTerm



    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];

    

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    
        for (NSMutableArray *array in contentsList)
        
            for (NSDictionary* dictionary in array)
            
                NSString *currentstring = [dictionary objectForKey:@"name"];
                NSRange r = [currentstring rangeOfString:searchTerm options:NSCaseInsensitiveSearch];
                if (r.location != NSNotFound) 
                    [[self searchResults] addObject:currentstring];
                
            
        
    



谁能告诉我哪里出错了?我假设它是 objectAtIndex 但哪个?

修订 - 我尝试稍微更改我的代码,现在我可以进行搜索,但原始视图上没有单元格标题,并且搜索结果在每个标题下重复!我现在的代码是:

- (void)handleSearchForTerm:(NSString *)searchTerm

    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];
    

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    
        for (NSMutableArray *array in contentsList)
        
            for (NSDictionary* dictionary in array)
            
                NSString *currentstring = [dictionary objectForKey:@"name"];
                NSRange r = [currentstring rangeOfString:searchTerm options:NSCaseInsensitiveSearch];
                if (r.location != NSNotFound) 
                    [[self searchResults] addObject:currentstring];
                
            
        
    


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    // Return the number of sections.
    if (tableView == [[self searchDisplayController] searchResultsTableView])
        return [self.stoneSections count];
    else
       return [self.stoneSections count];


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    return [self.stoneSections objectAtIndex:section];


#pragma mark -
#pragma mark UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section

    NSInteger rows;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        rows = [[self searchResults] count];
    else
        rows = [[self.contentsList objectAtIndex:section] count];


    return rows;


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath

    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = self.searchResults [indexPath.row];
    else
        contentForThisRow = [[[[self contentsList] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

    static NSString *CellIdentifier = @"stoneCell";

    UITableViewCell *cell = nil;
    if(mainTableView == self.mainTableView)
        cell = [mainTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.textLabel.text = self.searchResults [indexPath.row];
     else 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.text = [[[self.contentsList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
    

    return cell;

【问题讨论】:

调试提示: 1) 在调试器中单步执行,看看它死在哪里; 2)记录你调用objectAtIndex:的东西的价值;如果它是一个字符串而不是一个数组,那就有问题了; 3) 在 Objective-C 异常上设置断点,让调试器告诉你它在哪里遇到了错误。 【参考方案1】:

我遇到了同样的异常。我的问题的根本原因是我错误地引用了一个键/值对。在运行时,抛出异常“-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance”。这让我感到困惑的时间比我愿意承认的要长。但我找到了解决我的问题的方法,希望它可以帮助别人。引发异常的代码与更正后的代码一起出现。

错误代码:

- (void)testMyUnitTest

    WorldObject *worldObject;
    NSMutableDictionary *worldCoordinatesAll = [[NSMutableDictionary alloc] init];
    WorldCoordinate *localWCoordinate = [[WorldCoordinate alloc] init];

    // Populate with a world coordinate object
    [worldCoordinatesAll setObject:[localWCoordinate updateWithLatitude:X2LOC Longitude:Y2LOC Altitude:0] forKey:@"OBJECT 2"];

    for (id objectID in worldCoordinatesAll) 
        localWCoordinate = (WorldCoordinate *)objectID;
    localWCoordinate.latitude = LATITUDE_1;     // Exception being thrown on this line
    

更正后的代码:

- (void)testMyUnitTest

    WorldObject *worldObject;
    NSMutableDictionary *worldCoordinatesAll = [[NSMutableDictionary alloc] init];
    WorldCoordinate *localWCoordinate = [[WorldCoordinate alloc] init];

    // Populate with a world coordinate object
    [worldCoordinatesAll setObject:[localWCoordinate updateWithLatitude:X2LOC Longitude:Y2LOC Altitude:0] forKey:@"OBJECT 2"];

    for (id key in worldCoordinatesAll) 
        id value = [worldCoordinatesAll objectForKey:key];
        localWCoordinate = (WorldCoordinate *)value;
        localWCoordinate.latitude = LATITUDE_1;
    

我意识到,对于某些人来说,这似乎是一个完全明显的错误。但由于某种原因,我花了一个星期才看到它。所以我希望它可以帮助其他人节省一些时间。 :-)

【讨论】:

【参考方案2】:

要找出哪一行代码导致异常打开异常断点。

请参阅此问题"Run > Stop on Objective-C exception" in Xcode 4? 以获取有关如何打开异常断点的信息。

可能发生的情况是,您正在从没有方法 objectAtIndex: 的集合中拉出一个对象。下面一行

[[[self searchResults]objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];`

可能是问题的根源。在这里,您尝试在[[self searchResults]objectAtIndex:indexPath.section] 的结果上使用objectAtIndex:,但从我在- (void)handleSearchForTerm:(NSString *)searchTerm 中看到的内容来看,[self searchResults] 只包含一个字符串数组。这意味着您将方法 objectAtIndex: 应用于没有该方法的 NSString 实例。

【讨论】:

您还需要看什么?我可以把整个代码放进去,但它非常非常长 ok 运行它,它说此时它正在中断... if (tableView == [[self searchDisplayController] searchResultsTableView]) contentForThisRow = [[[[self searchResults] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"]; 是的,这似乎是问题所在,由于我不确定,我需要做些什么来纠正这个问题? 这取决于您要达到的目标。您希望所有搜索结果都在一个部分中还是在不同的部分中?您可能需要检查正确的部分,然后仅使用 indexPath.row 作为 objectAtIndex: 的参数,但这实际上取决于您想要做什么。 我想把它们放在不同的部分,所以说你正在搜索灰色,几个不同的部分都有灰色,所以我想让它们分开

以上是关于[__NSCFConstantString objectAtIndex:]:无法识别的选择器发送到实例 0x3141c的主要内容,如果未能解决你的问题,请参考以下文章

UITableView reloadData 崩溃并出现错误 [__NSCFConstantString objectForKey:]

[__NSCFConstantString objectAtIndex:]:无法识别的选择器发送到实例 0x3141c

NSInvalidArgumentException',原因:'-[__NSCFConstantString objectForKey:]:无法识别的选择器发送到实例 0x10256a1b0'

NSCFConstantString 大小尝试设置图像

type(obj) 和 obj.__class__ 的区别

0420模块 序列化模块 hashlib模块