Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么这些方法会泄漏NSDate对象?_Iphone_Objective C_Cocoa - Fatal编程技术网

Iphone 为什么这些方法会泄漏NSDate对象?

Iphone 为什么这些方法会泄漏NSDate对象?,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,第一个: + (NSDate*)convertToUTC:(NSDate*)sourceDate { NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFro

第一个:

+ (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;
}

第一个没有,但是第二个有,因为您创建了一个副本并且没有自动释放它。如果你以后不发布它,它将被泄露

我不明白你为什么要用第二种方法复制日期。只要把它剪掉,就可以修补漏洞了


您确实应该阅读(或重读),因为您似乎需要完善对内存管理规则的理解。

第一个没有,但第二个有,因为您创建了一个副本,但没有自动释放它。如果你以后不发布它,它将被泄露

我不明白你为什么要用第二种方法复制日期。只要把它剪掉,就可以修补漏洞了

您确实应该阅读(或重读),因为您似乎需要完善对内存管理规则的理解