gpt4 book ai didi

iphone - NSDateFormatter 导致僵尸灾难

转载 作者:行者123 更新时间:2023-11-28 17:46:57 25 4
gpt4 key购买 nike

我总是遇到一些问题,让臭名昭著的挑剔的 NSDateFormatter 导致我的代码内存不稳定。我不能掌握如何正确使用它。我查看了大量示例代码并在此之后对我的方法进行了建模,但似乎内存问题仍然困扰着我。我遇到的问题是这种方法正在创建一个僵尸 - 不太确定如何/为什么。一些见解会很棒!

-(NSString *)getTimeZoneFormatedDate:(int)subtractMinutes TimeZoneOffset:(NSString *)timeZoneOffset
{
float timeZoneOffsetInt = [timeZoneOffset floatValue];

//Calculate the requested UTC time
NSDate *UTCDateTimeNow = [NSDate date];
NSDate *UTCDateTimePast = [UTCDateTimeNow dateByAddingTimeInterval:((subtractMinutes*60)+(timeZoneOffsetInt*60*60))];

//Round the minutes down
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:UTCDateTimePast];
int minutes = [time minute];
float minuteUnit = floorf((float) minutes / 10);
minutes = minuteUnit * 10;

//Format the minutes string
NSString *minuteString;
minuteString = [NSString stringWithFormat:@"%d",minutes];
if ([minuteString length] < 2)
minuteString = [@"0" stringByAppendingString:minuteString];

//Format the rest of the date & time
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDateFormatter *dateFormatter;
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd, HH:"];
NSString *yearMonthDayHourString = [dateFormatter stringFromDate:UTCDateTimePast];

//Put the two together and return it!
return [yearMonthDayHourString stringByAppendingString:minuteString];
}

我是这样实现的:

        NSString *timeZoneText = [self getTimeZoneFormatedDate:minuteModifier*-10 TimeZoneOffset:radarTimeZoneOffset];

如果我在注释掉日期格式化程序的情况下运行我的项目并且我的方法刚刚返回:

return @"blah blah";

没有问题 - 一切都没有错误。所以,我相信可以安全地假设问题出在里面!感谢您的帮助!

最佳答案

我认为问题在于您的“NSString *yearMonthDayHourString”是自动释放的字符串。您可以将其保留在类似的实现代码中

self.timeZoneText = blabla 如果 timeZoneText 是保留的属性或者只是 [timeZoneText 保留];稍后发布;

关于iphone - NSDateFormatter 导致僵尸灾难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5256956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com