2012-03-20 18 views
2

私が欲しいもの:iOSの - コンテンツを検索し、ラベルに設定されたテキストクエリの結果に基づいて、HTML Webページをダウンロード

は、コンテンツ内を検索>NSStringに保存>サーバからコンテンツをWebページをダウンロードvia IsEqualToString>指定したキーワードが見つかったときにテキストをラベルに設定します。

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 

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]; 
} 

私がダウンロードしたWebページに目を通すときに自分自身が、私はエントリを参照してください。

platz_bespielbar

は、なぜ私の機能ドンはありません仕事は?

enter image description here

答えて

4

あなたは、文字列全体とその一部ではないだけをチェックしているからです。

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

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

する必要がありますが、それは

+0

おかげで多くのことができます願っています。今すぐ動作します。この部分が正確であることを説明するために1分もありますか? .location –

+2

rangeOfStringは、物の部分範囲(位置と長さ)を記述するためのより一般的なデータ構造であるNSRangeに答えます。しかし、部分文字列を検出するだけでは、場所が十分です。 – danh

関連する問題