2012-02-07 6 views
4

NSTimeIntervalでカウントダウンを試みました。しかし、毎回更新をリリースせずに間隔を変更できるようにしたい。そこで私は自分のウェブサイトからタイムインターバルをインポートしようとしました。私はNSTimeIntervalの数値をNSStringに保存しており、カウントダウンコードに実装するためにNSTimeIntervalに変換したいと考えています...NSStringをNSTimeIntervalに変換する

...それは動作しません。何か案は?

label.text = string; 
double timeInterval = [label.text doubleValue]; 
NSTimeInterval intervalForTimer = timeInterval; 
destinationDate = [[NSDate dateWithTimeIntervalSince1970:intervalForTimer] retain]; 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 

編集:

- (void)updateLabel { 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; 
    [dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components minute], 'm', [components second], 's']]; 

} 

私の新しいコードは以下のようになります。

NSLog(@"Downloaded file with contents: %@", string); 
    double timeInterval = [string doubleValue]; 
    label.text = [NSString stringWithFormat:@"%d", (int)timeInterval]; 
    NSTimeInterval intervalForTimer = timeInterval; 
    destinationDate = [[NSDate dateWithTimeIntervalSince1970:timeInterval] retain]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 

しかしdateLabelは何もしません...

+0

問題は何ですか?それはokと思われる –

+0

どのように動作していないのですか?文字列の値が正しく入力されていることを確認しましたか(デバッガを表示または見て確認していますか?)ラベルテキストが正しく設定されていることを確認しましたか? –

+0

label.textとは何ですか? – Shmidt

答えて

7

あなたのURL、http://gymnasium2.ai.ch/~mensa/countdown_timestamp.htmlは完全なHTMLファイルです。 NSString initWithContentsOfURL:は、その内容の全てを含む文字列を返します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>1328633219</title> 
</head> 

<body> 
1328633219 
</body> 
</html> 

これは、二重に変換することはできません。 HTMLを解析する必要があります。これはかなりの労力を要します。

それだけの数を含む単純なファイルを我慢する方が簡単です:

1328633219 

は次に上記のコードは変更せずに数を取得することができるだろう。

ただし、次のコードは、作業からあなたのコードを防ぐことがあります、その後、timeIntervalが正しく設定されませんnilある

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]]; 
label.text = string; // This line 
double timeInterval = [label.text doubleValue]; 

label場合、[label.text doubleValue]nilを返しますので。代わりに試してみてください:あなたは何が起こっているのを見ることができるように

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]]; 
double timeInterval = string; 
label.text = [NSString stringWithFormat:@"%d", (int)timeInterval]; 

あなたがファイルを取得した後にブレークポイントを落とし、またはのNSLog呼び出しを追加するために役立つだろう。

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]]; 
NSLog(@"Downloaded file with contents: %@", string); 

また、あなたはplistファイルをアップロードし、NSDictionary dictionaryWithContentsOfURL:またはNSArray arrayWithContentsOfURL:のようなものを使用することができます。 property list programming guideを参照してください。

+0

txtファイルで試してみましたが、htmlfileよりもうまくいくはずです。 – AmiiQo

+0

あなたのtxtファイルに数字だけが含まれている場合は、はい。 'label.text'から番号を取得しているので、labelがnilでないことを確認する必要があります。代わりに 'double timeInterval = [string doubleValue]'を試すことができます。 – mrb

+0

"Countdown [1538:f803]ダウンロードしたファイル:1354904016"というラベルが表示されていません... – AmiiQo

1

あなたがステップスルーでしたXcodeデバッガを使用してコードを実行すると、コードのどの行が予期しない結果になっているのか確認できますか?あなたはdestinationDateを使用することはありませんし、実際にあなたがコードの最後の行に渡すべきであることはされています。また、あなたからあなたの時間間隔の値を取得している場所はWebページある

timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

HTMLタグを使ってcountdown_timestamp.htmlcountdown_timestamp.txtに変更し、時間間隔の値だけを入力してください(「2.0」など)。 HTMLで囲んではいけません。

+0

updateLabelを追加しましたので、destinationDateの使用場所を見ることができます...しかし、それでもtxtファイルでは動作しません。 – AmiiQo

+0

あなたは壊れているようですIBOutlet。私はラベルがnilだと賭ける。したがって、label.textから値を取得しようとするコードは、0またはnilを返します。 –

2
NSTimeInterval timeInterval = [self timeIntervalFromString:@"00:01:40"]; 
NSLog(@"timeInterval %f", timeInterval); 

- (NSTimeInterval)timeIntervalFromString:(NSString *)string { 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"HH:mm:ss"]; 
NSDate *start = [dateFormatter dateFromString:@"00:00:00"]; 
NSDate *end = [dateFormatter dateFromString:string]; 
NSTimeInterval interval = [end timeIntervalSinceDate:start]; 
return interval; 
} 

出力:時間間隔100.000000

+0

のようなものが必要です。値が "66:66:66"の場合、終了値はnilになり、[end timeIntervalSinceDate:start];アプリをクラッシュさせる。 – ayalcinkaya

関連する問題