2012-02-07 153 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

什麼問題?它似乎好 – 2012-02-07 17:57:29

+0

它不工作?您是否驗證過(通過顯示或查看調試器)字符串值是否正確進入?您是否驗證過標籤文字設置正確? – 2012-02-07 17:58:40

+0

什麼顯示label.text? – Shmidt 2012-02-07 17:58:40

回答

7

您的網址,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 

然後上面的代碼將能夠獲得數量沒有任何變化。

但是,下面的代碼可能會阻止您的代碼工作:

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

如果labelnil,則timeInterval不會被正確設置,因爲[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 2012-02-07 18:09:15

+0

如果你的txt文件只包含沒有別的數字,是的。既然你從'label.text'中獲取數字,你必須確保標籤不是零。你可以試試'double timeInterval = [string doubleValue]'。 – mrb 2012-02-07 18:15:21

+0

它現在下載適當的文件:「倒計時[1538:f803]下載文件內容:1354904016」但標籤沒有顯示任何內容... – AmiiQo 2012-02-07 18:50:28

1

你通過步代碼(使用Xcode調試器)查看代碼的哪一行出現意外結果?你從來不會使用destinationDate,事實上,你應該傳遞給代碼的最後一行是:

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

而且,當你得到你的時間間隔值從一個網頁的地方與它周圍的HTML標籤。將countdown_timestamp.html更改爲countdown_timestamp.txt,並將其自身的時間間隔值(例如「2.0」)放入其中。不要用HTML包裝它。

+0

我添加了updateLabel,以便您可以看到我在哪裏使用destinationDate ...但它似乎仍然不能用於txt文件 – AmiiQo 2012-02-07 18:06:36

+0

聽起來好像你有一個破碎的IBOutlet中。我敢打賭標籤是零。因此,任何試圖從label.text獲取值的代碼都會返回零或零。 – 2012-06-26 01:07:20

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」,結束值將是零和[結束timeIntervalSinceDate:開始];崩潰應用程序。 – ayalcinkaya 2015-12-22 15:08:37