第一个:
+ (NSDate*)convertToUTC:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
return [NSDate dateWithTimeInterval:gmtInterval sinceDate:sourceDate];
}是的,我知道下一个很奇怪,但是我的服务器给了我一个疯狂的日期格式
+(NSDate *)getDateFromString:(NSString *)dtStr
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[inputFormatter setLocale:locale];
[locale release];
[inputFormatter setDateFormat:@"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [[inputFormatter dateFromString:dtStr] copy];
[inputFormatter release];
return formatterDate;
}发布于 2010-07-31 13:22:45
第一个没有,但第二个有,因为您创建了一个副本,并且没有自动释放它。如果你不稍后发布它,它将会泄露。
我不明白为什么要用第二种方法复制日期。只要把它剪掉就可以解决泄漏问题了。
您真的应该阅读(或重读) the Memory Management Programming Guide for Cocoa,因为您似乎需要提高对内存管理规则的理解。
https://stackoverflow.com/questions/3376855
复制相似问题