2013-06-24 45 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"]; 

我不明白上下文,但我在日誌中看到{},並且我猜測只有字典會以這種方式打印。