我想要的是:
从服务器下载网页 > 将内容保存在NSString
> 通过内容搜索IsEqualToString
> 找到指定关键字时将文本设置为标签。
我能够下载网页,在 a 中显示内容,UITextView
但我的if
函数总是执行 else 部分,这与我在IsEqualToString
. 请帮忙。
实际上我应该在我的标签中看到:课程是开放的,而不是抱歉,课程是关闭的。见截图。
//
// ViewController.h
// TestingHTML2
//
// Created by Marc Woerner on 20.03.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableData *myData;
NSURLConnection *myConnection;
}
@property (strong, nonatomic) IBOutlet UITextView *myTextView;
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
@end
视图控制器.m
//
// ViewController.m
// TestingHTML2
//
// Created by Marc Woerner on 20.03.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTextView;
@synthesize myLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
if (myConnection == nil) {
myData = [NSMutableData new];
NSString *urlString = [NSString stringWithFormat:@"http://www.golfplatz-altenstadt.de"];
myConnection =[NSURLConnection connectionWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:urlString]]
delegate:self];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[myData appendData:data];
}
- (void) initializeVariablesAgain {
myData = nil;
myConnection = nil;
myTextView = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *stringToLookup = [[NSString alloc]initWithData:myData encoding:NSASCIIStringEncoding];
if ([stringToLookup isEqualToString:@"platz_bespielbar"]) {
myLabel.text = @"Course is open";
} else {
myLabel.text = @"Sorry, course is closed";
}
myTextView.text = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];
[self initializeVariablesAgain];
}
当我自己浏览下载的网页时,我看到一个条目:
platz_bespielbar
那么为什么我的功能不起作用?