iphone - iOS - 下载 HTML 网页,搜索内容并根据查询结果将文本设置为标签

标签 iphone ios5 nsurlconnection nsmutabledata

我想要什么:

从服务器下载网页 > 将内容保存在 NSString 中 > 通过 IsEqualToString 搜索内容 > 找到指定关键字时将文本设置为标签。

我能够下载网页,在 UITextView 中显示内容,但我的 if 函数始终执行 else 部分,无论我在其中搜索什么IsEqualToString。请帮忙。

实际上我应该在我的标签中看到:类(class)开放,并且不抱歉,类(class)已关闭。请参阅屏幕截图。

//
//  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

ViewController.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

那么为什么我的函数不起作用?

enter image description here

最佳答案

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

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

应该是

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

希望对你有帮助

关于iphone - iOS - 下载 HTML 网页,搜索内容并根据查询结果将文本设置为标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788343/

相关文章:

ios - POST NSMutableURLRequest 中出现 405 错误

ios - NSURLRequest 下载,NSFileManager 看不到

nsurlconnection - 如何知道 NSURLResponse 是否来自缓存?

iphone - 制作我自己的 UIControl 事件并触发它?

iphone - AutoLayout 为多个 View 设置相同的高度和顶部空间,而不是垂直分布

iphone - UIGraphicsBeginImageContext 线程安全吗?

ios5 - 从动态原型(prototype)单元格显示弹出 View

ios - iPad 上列出的两个 "Calendar"条目 - 无法使用 EventKit 写入日历

iphone - CGBitmapContext在尝试调整图像大小时创建问题

ios - AVAudioRecorder - 正确的 MPEG4 AAC 录音设置