【发布时间】:2012-11-28 21:14:10
【问题描述】:
我有几个 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;
}
【问题讨论】:
-
@H2CO3 回答了您的问题。答案也与我的问题无关,我需要一种不同的方法
-
为什么需要转换为整数才能进行比较?如果你的字符串有前导零,你可以使用它们。
-
另外,为什么所有多余的演员表?
标签: iphone objective-c ios cocoa-touch nsdate