- [__ NSCFDictionary rangeOfString:]:发送到实例的无法识别的选择器 [英] -[__NSCFDictionary rangeOfString:]: unrecognized selector sent to instance
问题描述
我知道这里有很多解决方案可以解决这个问题,但是我被困在一条线路上,有时会成功运行n次崩溃,我真的不知道为什么会发生这种情况......
这是我发布邮件的代码,其中我收到错误 - [__ NSCFDictionary rangeOfString:]:无法识别的选择器发送到实例
这是我按下按钮时调用的方法代码。
NSString * ingredientLine = [arrayOfIngredientList objectAtIndex:i];
NSArray *拆分;
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);
//解析浮点数,如果它的1.00只变为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:@。0withString:@];
NSLog(@Amount is:%@,[split objectAtIndex:1]);
strAmount = [@stringByAppendingFormat:@%@%@,amnt,[split objectAtIndex:1]];
NSLog(@Ingredient is:%@,strAmount);
strIngedient = [split objectAtIndex:2];
}
else //成分头
{
// [[cell viewWithTag:10] setHidden:YES];
strIngedient = [split objectAtIndex:0];
}
}
其他
{
}
strIngredientsInfo = [strIngredientsInfo stringByAppendingFormat:@%@ - %@ < / BR>中,strAmount,strIngedient];
由于
NSArray *拆分;
NSRange range = [ingredientLine rangeOfString:@〜];
if(range.length> 0)
{
split = [ingredientLine componentsSeparatedByString:@〜];
}
请帮助。
<请说明为什么它会崩溃???? :(
这种情况正在发生,因为有时这段代码:
[arrayOfIngredientList objectAtIndex:i]
返回一个实例一个 NSDictionary
而不是你期望的 NSString
。这样做是因为预先在某个地方存储了 NSDictionary
在那个数组中。
所以,我不知道那个数组有多大以及打印整个内容是否切实可行看看发生了什么,但这里有一些东西可以帮助你调试。在它崩溃的部分,将其更改为:
if(![ingredientLine respondsToSelector:@selector(rangeOfString :)]){
NSLog(@ingredientLine不是NSString!它是:%@,ingredientLine);
} else {
NSRange range = [ingredientLine rangeOfString:@〜];
}
你也可以设置一个断点e NSLog
行看看发生了什么。请注意,这将阻止您的崩溃,但它不修复根本问题。这只是一个建议,可以帮助您调试真正的问题,这就是您在 arrayOfIngredientList中放置
NSDictionary
实例的某个地方。
编辑:对此处发生的事情的一些解释可能会对您有所帮助。 if
语句检查 ingredientLine
指向的对象是否不响应消息 rangeOfString:
。即使您已将 ingredientLine
声明为 NSString *
,您也可以轻松地将其分配给完全不同的实例class,在这种情况下它不再是 NSString
实例,它将无法响应 NSString
的消息。请注意,您也可以说:
`if(![ingredientList isKindOfClass:[NSString class]])`
在这里做同样的工作。但是我使用 respondsToSelector:
,因为这是在Objective C中了解的非常有用的信息。
i know many solutions are available here for this problem but I am stuck on a single line which runs sometime successfully n crashes sometimes, i really dont know why this is happening.....
here is my code of posting mail in which i m getting the error -[__NSCFDictionary rangeOfString:]: unrecognized selector sent to instance
and here is my code of the method which is called on button pressed.
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];
App crashes due to
NSArray* split ;
NSRange range = [ingredientLine rangeOfString:@"~"];
if (range.length > 0)
{
split = [ingredientLine componentsSeparatedByString:@"~"];
}
Please Help.
Please Suggest why it is crashing ???? :(
It is happening because sometimes this piece of code:
[arrayOfIngredientList objectAtIndex:i]
returns an instance of an NSDictionary
instead of the NSString
you are expecting. It does this because somewhere beforehand you have stored an NSDictionary
in that array.
So, I don't know how big that array is and whether it's practical to print its entire contents out to see what's happening, but here's something to help you debug. In the piece where it's crashing, change it to this:
if ( ! [ingredientLine respondsToSelector:@selector(rangeOfString:)] ) {
NSLog(@"ingredientLine is not an NSString! It is a: %@", ingredientLine);
} else {
NSRange range = [ingredientLine rangeOfString:@"~"];
}
You can also put a breakpoint on the NSLog
line to see what's happening. Note that this will stop your crashes, but it does not fix the underlying problem. This is just a suggestion to help you debug the real issue, which is that somewhere further up the line you are putting NSDictionary
instances in your arrayOfIngredientList
.
EDIT: Some explanation of what's happening here might help you. The if
statement checks to see whether the object pointed to by ingredientLine
does not respond to the message rangeOfString:
. Even though you've declared ingredientLine
as an NSString *
, you can easily assign it to an instance of a completely different class, in which case it won't be an NSString
instance anymore and it won't be able to respond to NSString
's messages. Note that you could also say:
`if ( ! [ingredientList isKindOfClass:[NSString class]] )`
Which would do the same job here. However I used respondsToSelector:
as it's a very useful message to know about in Objective C.
这篇关于 - [__ NSCFDictionary rangeOfString:]:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!