0

私はこの問題に対してここで多くの解決策が利用できることを知っていますが、私は時々正常に実行される単一の行で立ち往生しています。

これは、エラーが発生するメールを投稿する私のコードです-[__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:@"~"];
    }

助けてください。

クラッシュする理由を提案してください???? :(

4

1 に答える 1

2

時々このコードが原因で発生しています:

[arrayOfIngredientList objectAtIndex:i]

期待しているのNSDictionary代わりに のインスタンスを返します。NSStringこれを行うのは、事前NSDictionaryにその配列に を格納しているからです。

そのため、その配列がどのくらい大きいのか、何が起こっているのかを確認するために内容全体を出力することが実用的かどうかはわかりませんが、デバッグに役立つものを以下に示します。クラッシュしている部分で、これを次のように変更します。

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

行にブレークポイントを配置して、NSLog何が起こっているかを確認することもできます。これによりクラッシュは停止しますが、根本的な問題は修正されないことに注意してください。これは、実際の問題をデバッグするのに役立つ提案にすぎません。これは、NSDictionaryインスタンスをarrayOfIngredientList.

編集:ここで何が起こっているのかについての説明が役立つかもしれません。このステートメントは、 が指すオブジェクトがメッセージに応答しないifかどうかをチェックします。として宣言したとしても、完全に異なるクラスのインスタンスに簡単に割り当てることができます。その場合、インスタンスではなくなり、のメッセージに応答できなくなります。次のように言うこともできます。ingredientLinerangeOfString:ingredientLineNSString *NSStringNSString

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

ここで同じ仕事をするでしょう。ただしrespondsToSelector:、Objective C で知っておくと非常に便利なメッセージであるため、使用しました。

于 2011-12-23T14:05:45.440 に答える