2013-06-24 42 views
1

所以我有一个视图控制器,并在viewdidload方法它应该从网页加载一些内容(只是一个概念证明,它会最终缓存)。它使用tfhipple库获取内容,并将内容放入数组中,它将数据记录到控制台,然后我希望它将内容应用于UITextView。但是,当调用视图控制器时,它甚至会将文本记录到控制台,但是在它将其设置为UITextView的内容的行上会导致异常。NSLog字符串很好,但到UITextView字符串导致异常

NSData *dataURL; 
NSString *url = @"http://www.testwebsite.com/testpage.html"; 
dataURL = [NSData dataWithContentsOfURL: [NSURL URLWithString: url]]; 

NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 

TFHpple * doc  = [[TFHpple alloc] initWithHTMLData:dataURL]; 

NSArray *elements = [doc searchWithXPathQuery:@"//div[contains(@id,'main-section')]//text()"]; 

NSString * aboutcontents = [elements objectAtIndex:2]; 
NSLog(@"test: %@", aboutcontents); 
self.aboutbox.text = aboutcontents; 

它导致的异常情况如下,与前手控制台输出一起:

2013-06-24 09:37:51.433 AppName[24765:c07] test: { 
    nodeContent = "Test Content"; 
    nodeName = text; 
    raw = "Test Content"; 
} 
2013-06-24 09:37:51.434 AppName[24765:c07] -[TFHppleElement length]: unrecognized selector sent to instance 0x8043be0 
2013-06-24 09:37:51.434 AppName[24765:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TFHppleElement length]: unrecognized selector sent to instance 0x8043be0' 
*** First throw call stack: 
(0x25d012 0x16ebe7e 0x2e84bd 0x24cbbc 0x24c94e 0x76b4fb 0x3347 0x7111c7 0x711232 0x7328c9 0x732704 0x730bda 0x730a5c 0x732647 0x16ff705 0x6332c0 0x633258 0x855ff4 0x16ff705 0x6332c0 0x633258 0x6f4021 0x6f457f 0x6f4056 0x859af9 0x16ff705 0x6332c0 0x633258 0x6f4021 0x6f457f 0x6f36e8 0x662cef 0x662f02 0x640d4a 0x632698 0x22b2df9 0x22b2ad0 0x1d2bf5 0x1d2962 0x203bb6 0x202f44 0x202e1b 0x22b17e3 0x22b1668 0x62fffc 0x2fdd 0x21a5) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

我一点点坚持,为什么它这样做。如果我手动将字符串aboutcontents设置为某个值,那么它将更改UITextView的内容而不会出现问题。

任何帮助一如既往的赞赏。

+3

'aboutcontents'不是'NSString',这就是为什么它失败。 'NSLog'将在'NSObject'上调用一个方法,这就是为什么它仍然可以成功。 – borrrden

+0

TFHippleElement类的对象不支持'length'选择器。你为什么要发送一个TFHippleElement到期望NSString的东西? –

+0

试试这个self.aboutbox.text = [NSString stringWithFormat:@“%@”,aboutcontents] – CRDave

回答

1

你得到resutt的NSString * aboutcontents = [elements objectAtIndex:2];是一本字典,字典转换成字符串像这样

NSString * aboutcontents=[NSString stringWithFormat:@"%@",[elements objectAtIndex:2]]; 
+0

这工作出色。非常感谢! – Compy

+0

@亚当谢谢..... – NANNAV

2

试试这个:

TFHppleElement * aboutcontents = [elements objectAtIndex:2]; 
NSLog(@"test: %@", [aboutcontents text]); 
self.aboutbox.text = [aboutcontents text]; 

这里是hpple采取的文档部分:

TFHppleElement * element = [elements objectAtIndex:0]; 
[e text];      // The text inside the HTML element (the content of the first text node) 
[e tagName];     // "a" 
[e attributes];     // NSDictionary of href, class, id, etc. 
[e objectForKey:@"href"];  // Easy access to single attribute 
[e firstChildWithTagName:@"b"]; // The first "b" child node 

试图获得属性例如,看看它返回给你。

+0

固定为调用文本方法 – Tala

+0

嗨,谢谢,这不会导致崩溃但它返回null。 – Compy

+0

如果它返回null,那可能意味着该特定元素中没有文本。尝试在日志中显示的关键字之一。 @Adam – borrrden

1

试试这个。

NSDictionary * aboutcontents = [elements objectAtIndex:2]; 
NSLog(@"test: %@", aboutcontents); 
self.aboutbox.text = [aboutcontents objectForKey:@"nodeContent"]; 

我不明白上下文,但我在日志中看到{},并且我猜测只有字典会以这种方式打印。