Ios 用于转换字符串的现有Obj-C函数,例如;1m“&引用;1h"&引用;1d“;时间间隔?
我要解析一个字符串,它包含的时间段为“1ms”、“1s”、“1m”、“1h”、“1d”、“1w”、“1y” 我可以对它进行纹理分析,并使用case语句,而不需要太多麻烦,但我想知道是否有一种现有的方便方法可以用来代替它 ***30分钟后,我的问题没有答案,所以我用这段时间从头开始创建了一个函数,如下所示。但是,我仍然想知道是否有一个内置函数可以实现这一点Ios 用于转换字符串的现有Obj-C函数,例如;1m“&引用;1h"&引用;1d“;时间间隔?,ios,objective-c,Ios,Objective C,我要解析一个字符串,它包含的时间段为“1ms”、“1s”、“1m”、“1h”、“1d”、“1w”、“1y” 我可以对它进行纹理分析,并使用case语句,而不需要太多麻烦,但我想知道是否有一种现有的方便方法可以用来代替它 ***30分钟后,我的问题没有答案,所以我用这段时间从头开始创建了一个函数,如下所示。但是,我仍然想知道是否有一个内置函数可以实现这一点 - (NSTimeInterval) convertStringToNSTimeInterval:(NSString*) string {
- (NSTimeInterval) convertStringToNSTimeInterval:(NSString*) string
{
int digitCount = 0;
NSInteger charIdx = 0;
for (; charIdx<string.length; charIdx++)
{
unichar c = [string characterAtIndex:charIdx];
if(c >='0' && c <='9')
{
++digitCount;
}
}
NSString* baseString = [string substringToIndex: digitCount];
double base = [baseString doubleValue];
NSString* timeUnit = [string substringFromIndex:digitCount];
if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"Y"])
{
base *= 365.242199;
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"D"])
{
base *= (24 * 60 * 60);
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"H"])
{
base *= (60 * 60);
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"M"])
{
base *= 60;
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"S"])
{
// Already in seconds
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"W"])
{
base *= (7 * 24 * 60 * 60);
}
else if (NSOrderedSame == [timeUnit caseInsensitiveCompare:@"MS"])
{
base /= 1000;
}
NSTimeInterval result = base;
[Model log: [NSString stringWithFormat: @"convertStringToNSTimeInterval. Converting from: %@", string]];
[Model log: [NSString stringWithFormat: @"convertStringToNSTimeInterval. result: %f seconds", result]];
return result;
}
-(NSTimeInterval)convertStringToNSTimeInterval:(NSString*)字符串
{
int-digitCount=0;
NSInteger charIdx=0;
对于(;charIdx='0'&&c,如果您可以添加解决方案作为答案,我会这样做。如果您可以添加解决方案作为答案,我会这样做。