我有几个NSStrings
包含持续时间的,它们看起来像这样:@"03:40", @"08:40"
- 时间是一个动作的持续时间。我想int
为每个字符串设置一个分钟,比如“01:20”是“80”(分钟),以便稍后将它们相互比较。
你如何在 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;
}