2011-12-23 4 views
0

이 문제에 대해 많은 솔루션을 사용할 수 있지만 때때로 한 번 실행되는 한 줄에 붙어 있습니다. 가끔씩 충돌이 발생합니다. 왜 이런 일이 벌어지는 지 잘 모릅니다. ....- [__ NSCFDictionary rangeOfString :] : 인식 할 수없는 선택자를 인스턴스로 보냄

여기 -[__NSCFDictionary rangeOfString:]: unrecognized selector sent to instance

여기를 누르면 버튼에 호출되는 방법 내 코드 인 오류가 메신저하는 게시 메일 내 코드입니다.

NSString* ingredientLine = [arrayOfIngredientList objectAtIndex:i]; 

NSArray* split ; 

NSRange range = [ingredientLine rangeOfString:@"~"]; 

if (range.length > 0) 
{ 
    split = [ingredientLine componentsSeparatedByString:@"~"]; 

    if([split count] > 1) 
    { 
     float amount = [[split objectAtIndex:0] floatValue]; 

     float actualAmount = amount*((float)recipeServings/(float)4); 

     //parse the float if its 1.00 it becomes only 1 

     NSString* amnt = [NSString stringWithFormat:@"%.1f", actualAmount]; 

     NSArray* temp = [amnt componentsSeparatedByString:@"."]; 

     if([[temp objectAtIndex:1] isEqualToString: @"0"]) 

      amnt = [temp objectAtIndex:0]; 

     if(actualAmount == 0.0) 

      amnt = @""; 

     [amnt stringByReplacingOccurrencesOfString:@".0" withString:@""]; 

     NSLog(@"Amount is : %@",[split objectAtIndex:1]); 

     strAmount = [@"" stringByAppendingFormat:@"%@ %@",amnt,[split objectAtIndex:1]]; 

     NSLog(@"Ingredient is : %@", strAmount); 

     strIngedient = [split objectAtIndex:2]; 

    } 
    else //ingredients header 
    { 
     //[[cell viewWithTag:10] setHidden:YES]; 
     strIngedient = [split objectAtIndex:0]; 
    } 
} 
else 
{ 

} 

strIngredientsInfo = [strIngredientsInfo stringByAppendingFormat:@"%@ - %@ </br>",strAmount,strIngedient]; 

이 앱은

NSArray* split ; 

NSRange range = [ingredientLine rangeOfString:@"~"]; 

if (range.length > 0) 
{ 
    split = [ingredientLine componentsSeparatedByString:@"~"]; 
    } 

이 제발 도와주세요 인해 충돌합니다.

왜 충돌하는지 제안하시오 ????

[arrayOfIngredientList objectAtIndex:i] 

대신 NSString 당신이 기대하는 NSDictionary의 인스턴스를 반환 그것은이 작업을 수행 어딘가에 사전에 그에서 NSDictionary 저장되어 있기 때문에 : 코드 때때로이 작품이 때문에 :(

+2

'ingredientsLine '은 어디에서 왔습니까? 때로 NSString이 아닌 NSDictionary 인 것처럼 보입니다. – mattjgalloway

+0

Inderdientline을 추가했습니다. 인덱스 배열을 얻는 중 .. – NSException

+0

좋아요, 그럼'arrayOfIngredientList'는 어디서 오는 거죠? 거기에있는 유일한 객체는'NSString'입니까? (Gah 누군가가'arrayOfIngredientList'를 보여주기 위해 추가 한 비트를 제거했습니다!) – mattjgalloway

답변

2

일어나고있다. 배열.

배열이 얼마나 큰지, 전체 내용을 인쇄하여 실제 상황을 볼 수 있는지 여부는 알 수 없지만 디버깅에 도움이 될만한 내용이 있습니다. 충돌하는 부분에서 변경하십시오. 여기까지 :

if (! [ingredientLine respondsToSelector:@selector(rangeOfString:)]) { 
    NSLog(@"ingredientLine is not an NSString! It is a: %@", ingredientLine); 
} else { 
    NSRange range = [ingredientLine rangeOfString:@"~"]; 
} 

또한 NSLog 줄에 중단 점을 넣어서 어떤 일이 일어나고 있는지 확인할 수 있습니다. 이렇게하면 충돌이 멈추지 만 이 아니라이 기본 문제를 해결합니다. 이는 실질적인 문제를 디버그하는 데 도움이되는 제안 일 뿐이며 이는 에 NSDictionary 인스턴스를 추가하고있는 행의 위쪽 부분입니다.

편집 : 여기에 무슨 일이 일어 났는지에 대한 설명이 도움이 될 수 있습니다. if 문은 ingredientLine이 가리키는 개체가 메시지 rangeOfString:에 응답하지 않는지 확인합니다. ingredientLineNSString *으로 선언했지만 완전히 다른 클래스의 인스턴스에 쉽게 할당 할 수 있습니다.이 경우 더 이상 NSString 인스턴스가 아니며 NSString의 응답을받을 수 없습니다 메시지.

`if (! [ingredientList isKindOfClass:[NSString class]])` 

여기에서 동일한 작업을 수행 할 수 있습니다. 그러나 respondsToSelector:을 Objective C에서 알기에 매우 유용한 메시지로 사용했습니다.

+0

대단히 감사합니다! :) – NSException

+0

배열에 NSDictionary 인스턴스를 넣는 이유를 찾았습니까 ?? –

+0

+1 나 한테 ... : D –

관련 문제