-[__NSCFDictionary rangeOfString:]:发送到实例的无法识别的选择器
我知道这里有很多解决方案可以解决这个问题,但我被困在一条线上,有时会成功运行,有时会崩溃,我真的不知道为什么会发生这种情况......
这是我发布邮件的代码,其中我得到了错误 -[__NSCFDictionary rangeOfString:]: 无法识别的选择器发送到实例
这是我在按下按钮时调用的方法的代码。
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:@"~"];
}
“请帮忙”,应用程序崩溃了。
请建议为什么会崩溃??? :(
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 ???? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为有时这段代码:
返回一个
NSDictionary
实例,而不是您期望的NSString
。它这样做是因为您事先在该数组中存储了一个NSDictionary
。因此,我不知道该数组有多大,也不知道打印其全部内容以查看发生了什么,但这里有一些可以帮助您调试的内容。在崩溃的部分,将其更改为:
您还可以在
NSLog
行上放置一个断点来查看发生了什么。请注意,这将阻止您的崩溃,但它并不能解决根本问题。这只是一个帮助您调试真正问题的建议,即您将 NSDictionary 实例放入您的 arrayOfIngredientList 中。编辑:对这里发生的事情的一些解释可能会对您有所帮助。
if
语句检查ingredientLine
指向的对象是否不响应消息rangeOfString:
。即使您已将ingredientLine
声明为NSString *
,您也可以轻松地将其分配给完全不同的类的实例,在这种情况下,它不会是 < code>NSString 实例不再存在,它将无法响应NSString
的消息。请注意,您也可以说:这在这里会做同样的工作。不过,我使用了
respondsToSelector:
,因为它是 Objective C 中非常有用的消息。It is happening because sometimes this piece of code:
returns an instance of an
NSDictionary
instead of theNSString
you are expecting. It does this because somewhere beforehand you have stored anNSDictionary
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:
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 puttingNSDictionary
instances in yourarrayOfIngredientList
.EDIT: Some explanation of what's happening here might help you. The
if
statement checks to see whether the object pointed to byingredientLine
does not respond to the messagerangeOfString:
. Even though you've declaredingredientLine
as anNSString *
, you can easily assign it to an instance of a completely different class, in which case it won't be anNSString
instance anymore and it won't be able to respond toNSString
's messages. Note that you could also say:Which would do the same job here. However I used
respondsToSelector:
as it's a very useful message to know about in Objective C.