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 行。我这样做了,结果是第二块数据,但它返回了我在第 3 行和第 5 行用“,”连接的值。所以我再次用“,”分隔,这就是错误出现的地方。

所以,我需要的是第一块数据的内容全部分开。

任何帮助,将不胜感激..

谢谢!!!

4

1 回答 1

3

一个方便的技巧是设置一个断点,以捕获发生的任何异常。在 XCode 中,打开 Breakpoints 'tap',单击窗口底部的加号并选择“Add Exception Breakpoint...”。保留默认选项并点击“完成”。

接下来,异常告诉你它不喜欢什么:

原因:'-[ __NSArrayI componentsSeparatedByString:]: 无法识别的选择器发送到实例 0x7434eb0'

您正在将 componentsSeparatedByString 发送到 NSArray。(当然,您尝试将其称为 NSString,但事实并非如此。我很好奇在代码上运行“分析”是否会捕获此问题,我敢打赌它可能会。)

componentsSeparatedByString方法返回一个数组,您将其填充到另一个数组valoresFinais中。但是,您将项目从该数组中拉出并将它们称为NSString. 哎呀!将它们作为数组拉出,并迭代字符串数组,我怀疑您的代码将开始工作。

这只是对您的示例的快速阅读,但我认为这就是问题所在。祝你好运。

于 2013-01-19T16:17:08.637 回答