1

だから私はこれをテストしようとしています。基本的に、rawData.txt という名前のテキスト ファイルが含まれています。次のようになります。

    060315512 Name Lastname
    050273616 Name LastName

行を分割してから、個々の行を分割して最初の部分 (9 桁) を確認したかったのですが、まったく機能していないようです (ウィンドウが閉じます) このコードに問題はありますか?

    NSString *path = [[NSBundle mainBundle] pathForResource: @"rawData" ofType:@"txt"];
    if (path)
    {
        NSString *textFile = [NSString stringWithContentsOfFile:path]; 
        NSArray *lines = [textFile componentsSeparatedByString:(@"\n")];
        NSArray *line;
        int i = 0;
        while (i < [lines count])
        {
            line = [[lines objectAtIndex:i] componentsSeparatedByString:(@" ")];
            if ([[line objectAtIndex:0] stringValue] == @"060315512")
            {
                idText.text = [[line objectAtIndex: 0] stringValue];    
            }
            i++;
        }
    }
4

2 に答える 2

0

はい、2 つの文字列を比較する場合は、isEqualToString を使用する必要があります。これは、== が変数のポインター値を比較するためです。したがって、これは間違っています:

if ([[line objectAtIndex:0] stringValue] == @"060315512")

あなたは書くべきです:

if ([[[line objectAtIndex:0] stringValue] isEqualToString: @"060315512"])

于 2011-10-02T22:10:21.107 に答える
0

コンソール ログを確認すると、「stringValue sent to object (NSString) that does Respond」(またはそれらの効果) のようなものが表示される可能性があります。lineは文字列の配列なので、存在しないもの[[line objectAtIndex:0] stringValue]を呼び出そうとしています。-[NSString stringValue]

あなたは次のようなことを意味します:

NSString *path = [[NSBundle mainBundle] pathForResource: @"rawData" ofType:@"txt"];
if (path)
{
    NSString *textFile = [NSString stringWithContentsOfFile:path]; 
    NSArray *lines = [textFile componentsSeparatedByString:@"\n"];
    for (NSString *line in lines)
    {
        NSArray *columns = [line componentsSeparatedByString:@" "];
        if ([[columns objectAtIndex:0] isEqualToString:@"060315512"])
        {
            idText.text = [columns objectAtIndex:0];
        }
    }
}
于 2011-10-02T22:12:30.710 に答える