4

NSTimeIntervalでカウントダウンを試みました。ただし、毎回更新をリリースせずに間隔を変更できるようにしたいと思います。そこで、自分のWebサイトからTimeintervalをインポートしようとしました。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は何もしません...

4

3 に答える 3

7

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

これはdoubleに変換できません。HTMLを解析する必要がありますが、これは非常に多くの作業になる可能性があります。

数字だけを含む単純なファイルを作成する方が簡単です。

1328633219

そうすれば、上記のコードは変更なしで番号を取得できるようになります。

ただし、次のコードを使用すると、コードが機能しなくなる可能性があります。

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も返されるため、正しく設定されません。代わりに試してみてください:niltimeInterval[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:プロパティリストプログラミングガイドを参照してください。

于 2012-02-07T18:02:40.483 に答える
1

(Xcode デバッガーを使用して) コードをステップ実行して、コードのどの行が予期しない結果になっているのかを確認しましたか? を使用することはdestinationDateありません。実際、コードの最後の行に渡す必要があるのは次のとおりです。

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

また、時間間隔の値を取得する場所は、HTML タグがあちこちにあるWeb ページです。に変更countdown_timestamp.htmlしてcountdown_timestamp.txt、時間間隔の値をそのまま入れます (例: " 2.0")。HTML でラップしないでください。

于 2012-02-07T17:58:19.863 に答える