【问题标题】:Hpple implementation/Unrecognized selectorhpple 实现/无法识别的选择器
【发布时间】:2012-04-06 08:15:30
【问题描述】:

我在这里使用 hpple html 解析器:https://github.com/topfunky/hpple

为了测试这个函数,我将它添加到一个简单的项目中,并且能够编译和打开模拟器而没有错误,但是当它被调用时,我得到一个无法识别的选择器错误。

//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 made”显示,然后我收到这个无法识别的选择器消息:

-[__NSCFString bytes]: 无法识别的选择器发送到实例 0x6a52d60

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString bytes]: unrecognized selector sent to instance 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) 终止称为抛出异常

我知道它不喜欢某些东西,但是是什么导致了故障,或者是否需要额外的框架/导入才能正确执行?就目前而言,我将 UIKit、viewcontroller.h 和 TFHpple.h 设置为该文件中的唯一导入。

【问题讨论】:

    标签: objective-c ios xcode parsing html-parsing


    【解决方案1】:

    这是你的问题:

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

    TFHppleinitWithHTMLData 应该采用NSData。您将htmlData 声明为NSData,但您分配给它的实际对象是NSString

    这应该可以解决它:

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

    【讨论】:

    • 谢谢!现在我在不同的地方坠毁了……大脑的谜题还在继续。感谢您的解释,我一定记得下次使用 NSData。
    • 我的荣幸。您在上面粘贴的其余代码看起来不错。另外,我建议你修复的那行应该是发出警告的。修复所有这些总是一个好主意,尤其是当您的程序崩溃时。附言随意点击复选标记接受我的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2018-01-22
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多