2012-11-30 57 views
0

我試圖讓搜索顯示控制器的工作和剛剛得到這個錯誤:[__NSCFConstantString objectAtIndex:]:無法識別的選擇發送到實例0x3141c

2012-11-30 14:48:19.903 searchstory[5812:c07] -[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x3141c 2012-11-30 14:48:19.904 searchstory[5812:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x3141c' * First throw call stack: (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: terminate called throwing an exception

我已經玩有關的代碼試圖讓這工作。代碼我有搜索和爲推子是:

- (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; 
} 
+0

調試提示:1)在調試器中逐步瀏覽並查看它在哪裏死亡; 2)記錄你調用'objectAtIndex:'的東西的值;如果它是一個字符串而不是一個數組,這是一個問題; 3)在Objective-C異常中設置一個斷點,並讓調試器告訴你它遇到錯誤的位置。 –

回答

2

我遇到了同樣的異常。我的問題的根本原因是我錯誤地引用了鍵/值對。在運行時,正在拋出異常「 - [__ NSCFConstantString objectAtIndex:]:無法識別的選擇器發送到實例」。這讓我感到困惑的時間比我想要承認的要長。但是我找到了解決問題的方法,希望它能幫助別人。拋出異常的代碼跟隨糾正的代碼。

錯誤代碼:

- (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; 
    } 
} 

我知道一些,這似乎是一個完全明顯的錯誤。但由於某種原因,我花了一個星期纔看到它。所以我希望它能幫助別人節省一些時間。 :-)

0

要找出哪一行代碼負責異常打開異常斷點。

有關如何打開異常斷點的信息,請參見此問題"Run > Stop on Objective-C exception" in Xcode 4?

可能發生的情況是,您正在將某個對象拉出沒有方法objectAtIndex:的集合。以下行

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

可能是問題的根源。在這裏,您嘗試使用objectAtIndex:上的[[self searchResults]objectAtIndex:indexPath.section]的結果,但從我在- (void)handleSearchForTerm:(NSString *)searchTerm中可以看到的結果中,[self searchResults]只包含一串字符串。這意味着您將方法objectAtIndex:應用於不具有該方法的NSString實例。

+0

你還需要看什麼?我可以把整個代碼,但它非常非常長 – David

+0

確定運行,它說它在這一點上打破... \t if(tableView == [[self searchDisplayController] searchResultsTableView]) \t \t contentForThisRow = [[[[ self searchResults] objectAtIndex:indexPath.section] objectAtIndex:indexPath。row] objectForKey:@「name」]; – David

+0

是的,看起來似乎是問題所在,我不知道該怎麼修正這個問題? – David

相關問題