Cocoa中NSString到NSDate的处理

Cocoa中NSString到NSDate的处理,第1张

概述NSDate是NS类库中基础类型之一。随着数字化发展,程序对数据处理量越来越大,我们经常从服务器取得的日期是字符串序列,格式化为正确的date类型是一个不可避免的工作。在Cocoa程序里提供了非常方便的函数和类,但是仍然需要我们了解一些技巧。尤其是当我们的程序面对大量的日期字符串转换的时候,要格外的注意。苹果文档中使用NSDateFormatter类格式化日期字符串,但是以防读者不知道,我这里提一

NSDate是NS类库中基础类型之一。随着数字化发展,程序对数据处理量越来越大,我们经常从服务器取得的日期是字符串序列,格式化为正确的date类型是一个不可避免的工作。在Cocoa程序里提供了非常方便的函数和类,但是仍然需要我们了解一些技巧。尤其是当我们的程序面对大量的日期字符串转换的时候,要格外的注意。苹果文档中使用NSDateFormatter类格式化日期字符串,但是以防读者不知道,我这里提一下:它的速度非常慢!!这篇文章介绍如何处理这种情况。

1234567891011121314
- (NSDate *)dateFromString:(Nsstring *)string {  if (!string) {  return nil;  }  //Wed Mar 14 16:40:08 +0800 2012  static NSDateFormatter *dateformatter=nil;  if(dateformatter==nil){  dateformatter = [[NSDateFormatter alloc] init];  NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  [dateformatter setTimeZone:tz];  [dateformatter setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];  }  return [dateformatter dateFromString:string]; } 

由于NSDateFormatter内部代码原因,所以格式化字符串代价很大。对于个别地方使用它做日期转换是非常方便的,但是如果是放在一个大的循环内部,直接使用NSDateFormatter绝对不是明智的选择。它很有可能成为拖慢你程序速度的元凶。

根据这篇博客 How to Drastically Improve Your App with an Afternoon and Instruments 编写c代码格式化时间字符串速度会提高不少。

123456789101112
- (NSDate *)dateFromISO8601String:(Nsstring *)string {  if (!string) return nil;   struct tm tm;  time_t t;   strptime([string cStringUsingEnCoding:NSUTF8StringEnCoding], "%Y-%m-%dT%H:%M:%s%z", &tm);  tm.tm_isdst = -1;  t = mktime(&tm);   return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]]; } 

其实,虽然看似使用c语言格式化时间速度很快,但实际上strptime内部仍然需要parser你的格式化字符串,该计算同样耗时。所以上述代码并不是最快的。其实如果你知道你的程序将会取得什么格式的日期字符串,那么直接字符串处理后利用NSCalendar和NSDateComponents可以更快。

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
- (NSDate*)dateFromString:(Nsstring *)string; {  //Wed Mar 14 16:40:08 +0800 2012  if (!string) return nil;   static NSCalendar *gregorian=nil;  static NSDateComponents *comps=nil;  static NSDictionary *month=nil;  if (gregorian==nil) {  gregorian = [[NSCalendar alloc] initWithCalendarIDentifIEr:NSGregorianCalendar];  comps = [[NSDateComponents alloc] init];  month = [NSDictionary dictionaryWithObjectsAndKeys:  [NSNumber numberWithInt:1], @"Jan",  [NSNumber numberWithInt:2], @"Feb",  [NSNumber numberWithInt:3], @"Mar",  [NSNumber numberWithInt:4], @"Apr",  [NSNumber numberWithInt:5], @"May",  [NSNumber numberWithInt:6], @"Jun",  [NSNumber numberWithInt:7], @"Jul",  [NSNumber numberWithInt:8], @"Aug",  [NSNumber numberWithInt:9], @"Sep",  [NSNumber numberWithInt:10], @"Oct",  [NSNumber numberWithInt:11], @"Nov",  [NSNumber numberWithInt:12], @"Dec",  nil];  }   NSArray *a=[string componentsSeparatedByString:@" "];  if ([a count]==6) {  @try {  [comps setYear:[[a objectAtIndex:5] intValue]];  [comps setMonth:[[month objectForKey:[a objectAtIndex:1]] intValue]];  [comps setDay:[[a objectAtIndex:2] intValue]];  Nsstring *time=[a objectAtIndex:3];  [comps setHour:[[time substringWithRange:NSMakeRange(0, 2)] intValue]];  [comps setMinute:[[time substringWithRange:NSMakeRange(3, 2)] intValue]];  [comps setSecond:[[time substringWithRange:NSMakeRange(6, 2)] intValue]];  }  @catch (NSException *exception) {  }  @finally {  }  return [gregorian dateFromComponents:comps];  }  return [NSDate date]; } 

可以看到单独解析字符串的方法,虽然代码要长,但实际上它的速度是最快的。

最后作为参考资料,说明一下 OSX 10.6 下 NSDateFormatter 使用 Unicode Locale Data MarkuP Language (LDML) version tr35-10 标准。作为标准文档,Apple是不会全部写到开发文档里的,不明白的同学也不用抱怨。

总结

以上是内存溢出为你收集整理的Cocoa中NSString到NSDate的处理全部内容,希望文章能够帮你解决Cocoa中NSString到NSDate的处理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1059875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存