objective-c - NSString 到 TimeInterval

标签 objective-c ios

我有一个格式为 00:00:00.00 的 NSString最初来自 [dateFormatter setDateFormat:@"HH:mm:ss.SS"];

我如何获取该字符串并将其转换为 TimeInterval?

最佳答案

如果您 100% 绝对肯定这种格式永远不会改变,那么这里有一个使用 one of the most useful classes 的快速'n'dirty 解决方案在 cocoa 中:

- (NSTimeInterval)timeFromString:(NSString *)str
{
    NSScanner *scn = [NSScanner scannerWithString:str];

    int h, m, s, c;
    [scn scanInt:&h];
    [scn scanString:@":" intoString:NULL];
    [scn scanInt:&m];
    [scn scanString:@":" intoString:NULL];
    [scn scanInt:&s];
    [scn scanString:@"." intoString:NULL];
    [scn scanInt:&c];

    return h * 3600 + m * 60 + s + c / 100.0;
}

关于objective-c - NSString 到 TimeInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748007/

相关文章:

iphone - 如何在IOS中动态调用类方法?

ios - 在呈现另一个 ViewController 时关闭 ViewController

ios - JSON 响应中 NSDictionary 内的对象数组 - Objective c

ios - 如何将 "SignalProducer<Bool, NoError>"转换为 ReactiveCocoa 3 的 "SignalProducer<Bool, NSError>"?

ios - 为什么我无法圆化 UIImageView 的边缘?

iphone - 我可以在不使用MDM的情况下在iOS Developer Enterprise Program上使用APNS吗

objective-c - 传递给 View 的 IOS 值丢失或被遗忘

ios - 如何使用带有可选毫秒部分的 NSDateFormatter 解析 ISO 8601

iphone - Restkit 使用 Forsquare 映射嵌套数组

ios - CGPoint的小数部分是什么意思?