2012-03-21 60 views
0

我与hpple HTML解析器在这里工作:https://github.com/topfunky/hppleHpple执行/无法识别的选择

为了测试我已经把它添加到一个简单的项目功能,我能够没有错误编译并打开模拟器,但是当它被调用,我得到一个无法识别的选择器错误。

//THIS ACTION SET TO RUN WITH THE PUSH OF A BUTTON 

- (IBAction)parseElements{ 

NSString *urlRequest = item.link; 

NSLog(@"urlRequest defined."); 

NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"htmlData created."); 

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; 

NSLog(@"xpathParser made."); 

NSString *queriedItem = @"[@class='title']"; 

// THE APP FAILS WHILE TRYING TO EXECUTE THE NEXT LINE 

NSArray *elements = [xpathParser searchWithXPathQuery:queriedItem]; 

NSLog(@"elements searched."); 

TFHppleElement *element = [elements objectAtIndex:0]; 

NSLog(@"element recalled."); 

NSString *storyTitle = [element content]; 

NSLog(@"The title of the story is: %@", storyTitle); 

} 

的NSLogs管理通过 “xpathParser制造” 来显示,然后我收到此无法识别的选择信息:

- [__ NSCFString字节]:无法识别的选择发送到实例0x6a52d60

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString bytes]:无法识别的选择程序发送到实例0x6a52d60'

*第一掷调用堆栈: (0x16c8052 0x1859d0a 0x16c9ced 0x162ef00 0x162ece2 0x495c 0x352e 0x2e3f 0x16c9ec9 0x1395c2 0x13955a 0x1deb76 0x1df03f 0x1de2fe 0x15ea30 0x15ec56 0x145384 0x138aa9 0x15b2fa9 0x169c1c5 0x1601022 0x15ff90a 0x15fedb4 0x15feccb 0x15b1879 0x15b193e 0x136a9b 0x2658 0x25b5) 终止叫做抛出异常

我知道它不喜欢SOMETHING,但是是什么导致了小故障或是正确执行所需的附加框架/进口?现在我有UIKit,viewcontroller.h和TFHpple.h设置为该文件中唯一的导入。

回答

1

这是你的问题:

NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil]; 

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; 

TFHppleinitWithHTMLData应该采取NSData。您声明htmlDataNSData,但您分配给它的实际对象是NSString

这应该修复它:

NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: urlRequest]]; 
+0

谢谢!现在我在不同的领域崩溃......而大脑的难题仍在继续。欣赏解释,下次我一定记得使用NSData。 – 2012-03-21 18:57:43

+0

我的荣幸。上面粘贴的其他代码看起来很好。另外,我建议你修正的路线应该会发出警告。修复所有这些问题总是一个好主意,尤其是在程序崩溃的情况下。 P.S.请随时通过点击对勾来接受我的答案:) – yuji 2012-03-21 19:01:22

相关问题