gpt4 book ai didi

objective-c - Hpple 实现/无法识别的选择器

转载 作者:行者123 更新时间:2023-11-29 04:44:46 24 4
gpt4 key购买 nike

我正在使用 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]: unrecognized selector sent to instance 0x6a52d60

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x6a52d60'

* First throw call stack: (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) terminate called throwing an exception

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

最佳答案

这是你的问题:

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]];

关于objective-c - Hpple 实现/无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9810926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com