所以我松散地按照本教程将 HTML 解析为 Master/Detail 应用程序。
我正在尝试将 NSArray 更改为 NSString,但唯一的数组元素(只有一个元素)似乎没有被完全解析。
我的代码:
视图控制器.h:
#import <UIKit/UIKit.h>
@interface viciViewController : UIViewController {
IBOutlet UITextView *label;
IBOutlet UITextField *textfield;
IBOutlet UITextView *translation;
IBOutlet UIButton *transButton;
BOOL connectedToInternet;
}
-(IBAction)translateButton;
@end
视图控制器.m:
#import "viciViewController.h"
#import "TFHpple.h"
@interface viciViewController ()
@end
@implementation viciViewController
- (BOOL)connectedToInternet
{
NSURL *url=[NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
return ([response statusCode]==200)?YES:NO;
}
-(IBAction)translateButton {
if ([self connectedToInternet] == NO)
{
translation.text = @"Sorry, you need an internet connection to translate.";
// Not connected to the internet
}
else
{
// Connected to the internet
[textfield resignFirstResponder];
NSString *toTranslate = self->textfield.text;
NSString *url = [NSString stringWithFormat:@"http://www.archives.nd.edu/cgi-bin/wordz.pl?keyword=%@", toTranslate];
NSURL *translationUrl = [NSURL URLWithString:(url)];
NSData *translationHtmlData = [NSData dataWithContentsOfURL:translationUrl];
TFHpple *translationParser = [TFHpple hppleWithHTMLData:translationHtmlData];
// 3
NSString *translationXpathQueryString = @"/html/body/pre";
NSArray *translationOutput = [translationParser searchWithXPathQuery:translationXpathQueryString];
NSString * result = [[translationOutput valueForKey:@"description"] componentsJoinedByString:@""];
NSLog(@"Response %@", result);
}
控制台中的日志说:
Response {
nodeChildArray = (
{
nodeContent = "\nsemper ADV POS \nsemper ADV [XXXAX] \nalways;\n\n";
nodeName = text;
}
);
nodeName = pre;
raw = "<pre>\nsemper ADV POS \nsemper ADV [XXXAX] \nalways;\n\n</pre>";
}
所以,问题:是否可以将nodeContent作为 NSString 并将其\n
转换为回车符?
感谢所有的帮助!