Domanda

So I have a view controller and in the viewdidload method it's supposed to load some content from a webpage (just a proof of concept, it'll be cached eventually). It gets the content using the tfhipple library and puts the contents into an array, it logs the data to the console and then I want it to apply the contents to a UITextView. However when the view controller is called it gets so far as to log the text to the console but on the line where it sets it as the contents of the UITextView it causes an exception.

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;

The exception it causes is as follows, along with the console output before hand:

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) 

I'm a little bit stuck as to why it does this. If I manually set the string aboutcontents to something then it changes the contents of the UITextView without issue.

Any help is as always appreciated.

È stato utile?

Soluzione 2

you getting resutt for NSString * aboutcontents = [elements objectAtIndex:2]; is a dictionary,convert dictionary into string like this

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

Altri suggerimenti

Try this:

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

Here is the documentation part taken from 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

Try to get attributes for example and see what it returns to you.

Try this.

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

I don't understand the context but I saw { } in the Log and I guess only dictionaries get printed that way.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top