1

ウェブサイトからいくつかの株式市場データをインポートしましたが、それらを分離するのに問題があります。コードの一部では正常に機能していますが、最終的には機能していません。コードは次のとおりです。

NSURL *url = [NSURL URLWithString:@"http://www.bloomberg.com/quote/USIM5:BZ"];

NSData *webData = [NSData dataWithContentsOfURL:url];

//NSString *xPathQuery = @"//h3[@class=''] |//span[@class=' price'] | //span[@class=' trending_up up'] | //span[@class=' trending_up up']/span | //table[@class='snapshot_table']/tr/th | //table[@class='snapshot_table']/tr/td";

NSString *xPathQuery = @"//span[@class=' price'] | //span[@class=' trending_up up'] | //span[@class=' trending_up up']/span | //table[@class='snapshot_table']/tr/td";

TFHpple *parser = [TFHpple hppleWithData:webData isXML:NO];

NSArray *array = [parser searchWithXPathQuery:xPathQuery];

valores = [[NSMutableArray alloc]init];
for (TFHppleElement *element in array) {
    [valores addObject:[[element firstChild] content]];
}

novosValores = [[NSMutableArray alloc]init];
for (NSString *valuesDatum in valores) {
    NSString *removeNewLine = [[valuesDatum componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
    NSString *removeSpace = [removeNewLine stringByReplacingOccurrencesOfString:@"         " withString:@""];
    NSString *removeSpaceOne = [removeSpace stringByReplacingOccurrencesOfString:@"    " withString:@""];
    [novosValores addObject:removeSpaceOne];
}

NSLog(@"%@",novosValores);

valoresFinais = [[NSMutableArray alloc]init];
for (NSString *valuesDatum in novosValores) {
    NSArray *val = [valuesDatum componentsSeparatedByString:@" - "];
    [valoresFinais addObject:val];
}

NSLog(@"%@",valoresFinais);

infos = [[NSMutableArray alloc]init];
for (NSString *dados in valoresFinais) {
    NSArray *arrayDados = [dados componentsSeparatedByString:@","];
    [infos addObject:arrayDados];
}
NSLog(@"%@", infos);
}

ログは私にこれを示しています:

2013-01-19 12:45:17.526 BloombergQuotes[1720:c07] (
"12.300",
"12.580",
"12.270 - 12.590",
"4,572,600",
"12.460",
"5.570 - 13.770",
"+7.82%"
)
2013-01-19 12:45:17.528 BloombergQuotes[1720:c07] (
    (
    "12.300"
),
    (
    "12.580"
),
    (
    "12.270",
    "12.590"
),
    (
    "4,572,600"
),
    (
    "12.460"
),
    (
    "5.570",
    "13.770"
),
    (
    "+7.82%"
)
)
2013-01-19 12:45:17.528 BloombergQuotes[1720:c07] -[__NSArrayI    componentsSeparatedByString:]: unrecognized selector sent to instance 0x7434eb0
2013-01-19 12:45:17.529 BloombergQuotes[1720:c07] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[__NSArrayI componentsSeparatedByString:]:  unrecognized selector sent to instance 0x7434eb0'
*** First throw call stack:
(0x2101012 0x11fae7e 0x218c4bd 0x20f0bbc 0x20f094e 0x2fbe 0x223817 0x223882 0x172a25   0x172dbf 0x172f55 0x17bf67 0x13ffcc 0x140fab 0x152315 0x15324b 0x144cf8 0x205cdf9 0x205cad0   0x2076bf5 0x2076962 0x20a7bb6 0x20a6f44 0x20a6e1b 0x1407da 0x14265c 0x215d 0x2085)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

したがって、データの最初のチャンクは、「-」で区切る必要がある3行目と5行目を除いて、必要なものにはほぼ問題ありません。これを実行すると、結果はデー​​タの2番目のチャンクになりますが、3行目と5行目に「、」で接続された値が返されました。それで、私は再び「、」で区切られました、そしてそれはエラーが来るところです。

したがって、必要なのは、データの最初のチャンクの内容がすべて分離されていることです。

どんな助けもいただければ幸いです。

ありがとう!!!

4

1 に答える 1

3

便利なヒントの1つは、例外が発生した場合にそれをキャッチするブレークポイントを設定することです。XCodeで、ブレークポイントの「タップ」を開き、ウィンドウの下部にあるプラス記号をクリックして、「例外ブレークポイントの追加...」を選択します。デフォルトのオプションを保持し、「完了」をクリックします。

次に、例外はそれが好きではないことをあなたに伝えることです:

理由:'-[ __NSArrayI componentsSeparatedByString:]: 認識されないセレクターがインスタンス0x7434eb0に送信されました'

componentsSeparatedByStringをNSArrayに送信しています。(確かに、NSStringと呼ぼうとしましたが、そうではありません。コードで「Analyze」を実行するとこの問題が発生するかどうか知りたいです。きっとそうかもしれません。)

このcomponentsSeparatedByStringメソッドは、別の配列に詰め込んでいる配列を返しますvaloresFinais。ただし、その配列からアイテムを引き出して、それらをと呼びますNSString。おっと!それらを配列として引き出し、その文字列の配列を繰り返します。コードが機能し始めると思います。

これはあなたの例をざっと読んだだけですが、それが問題だと思います。幸運を祈ります。

于 2013-01-19T16:17:08.637 に答える