2010-06-14 98 views
1

我想弄清楚如何創建一個使用時間值作爲條件的'if'語句。例如:iPhone時間比較

if (time <= 10:00) { 
    score = 3; 
} else if (time <= 20:00) { 
    score = 5; 
} else { 
      score = 9; 
} 

我知道,「5:23」的字符串所不能比擬的這種方式,但我不認爲我可以把這樣的一個字符串值直接轉換成整數。有什麼想法嗎?

+0

這使我懷疑00:00 <24:00。你可能想檢查'NSDateFormatter'&' - [NSDate dateFromString:]'out? – 2010-06-14 05:43:40

回答

1

試試這個:

NSString *realTimeString = @"10:00"; 
NSString *someTimeString = [realTimeString stringByReplacingOccurrencesOfString:@":" 
              withString:@"."]; 
float time = [someTimeString floatValue]; 

if (time <= 10.00) { 
    score = 3; 
} else if (time <= 20.00) { 
    score = 5; 
} else { 
    score = 9; 
}