2

我想要的是:

从服务器下载网页 > 将内容保存在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

那么为什么我的功能不起作用?

在此处输入图像描述

4

1 回答 1

4

这是因为您正在检查整个字符串,而不仅仅是其中的一部分。

if ([stringToLookup isEqualToString:@"platz_bespielbar"]) {

应该

if ([stringToLookup rangeOfString:@"platz_bespielbar"].location != NSNotFound]) {

希望能帮助到你

于 2012-03-20T14:06:45.350 回答