1

だから私はView Controllerを持っており、viewdidloadメソッドではWebページからいくつかのコンテンツをロードすることになっています(概念実証にすぎず、最終的にキャッシュされます)。ライブラリを使用してコンテンツを取得し、コンテンツをtfhipple配列に入れ、データをコンソールに記録してから、コンテンツをUITextView. ただし、View Controller が呼び出されると、テキストをコンソールに記録するところまで到達しますが、それをコンテンツとして設定する行で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問題なく内容が変更されます。

どんな助けでもいつも感謝しています。

4

3 に答える 3

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

たとえば、属性を取得して、それが返すものを確認してください。

于 2013-06-24T08:51:03.467 に答える
1

これを試して。

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

コンテキストはわかりませんが、ログに { } が表示されました。そのように出力されるのは辞書だけだと思います。

于 2013-06-24T09:01:10.777 に答える