我有幾個NSStrings
包含持續時間,他們看起來像這樣:@"03:40", @"08:40"
- 時間是一個行動的持續時間。 我希望有一個int
分鐘的每個字符串,說「01:20」是「80」(分鐘),以便稍後將它們相互比較。轉換HH:MM NSString到NSInteger與分鐘
你如何在Objective-C中管理這個簡單的東西? 在此先感謝
編輯:
試過了,沒有用。我想要一個完全不同的方法,輕鬆簡單。
- (NSMutableArray *)sortResultsByTime
{
NSMutableArray *sortedTrips = [NSMutableArray arrayWithArray:[_tweets sortedArrayUsingComparator: ^(id trip1, id trip2) {
double time1 = [self durationFromString:[(Tweet *) trip1 flightDuration]];
double time2 = [self durationFromString:[(Tweet *) trip1 flightDuration]];
NSLog(@"What's time1? It's %f", time1);
NSLog(@"What's time2? It's %f", time2);
if (time1 < time2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (time1 > time2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}]];
return sortedTrips;
}
- (double)durationFromString:(NSString *)durationString
{
NSArray *durationArray = [durationString componentsSeparatedByString:@":"];
return [durationArray[0] doubleValue] + [durationArray[1] doubleValue]/60.0;
}
[?你嘗試過什麼(http://whathaveyoutried.com ) – 2012-11-28 21:15:36
@ H2CO3回答了你的問題。此外,答案與我的問題無關,我需要一個不同的方法 –
爲什麼你需要轉換爲整數以便比較?如果你的字符串有前導零,你可以使用它們。 – djromero