gpt4 book ai didi

iphone - 错误 : can't allocate region in iOS

转载 作者:行者123 更新时间:2023-11-29 10:56:21 25 4
gpt4 key购买 nike

只要通过调用此方法选择一行 UIPickerView,我就会将对象添加到可变数组中-

-(void)setScheduleStartDate:(NSString *)dateStr
{
[scheduleDatesArray removeAllObjects];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
df.dateFormat = @"d MMMM yyyy";
scheduleStartDate = [df dateFromString:dateStr];

/******* getting array of schedule dates ***********/

NSDate* scheduleEndDate = [scheduleStartDate dateByAddingTimeInterval:60*60*24*28*6]; // add six month (of 28 days) in schedule start date
double endMS = [scheduleEndDate timeIntervalSinceDate:scheduleStartDate];

for (double i =0; i < endMS; i = (i + 60*60*24*14)) {

[scheduleDatesArray addObject:[NSNumber numberWithDouble:i]];
}
}

多次调用此方法后,我崩溃并显示此错误消息

malloc: *** mmap(size=627101696) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

通过在 malloc_error_break 中设置断点,我的应用程序进入 for 循环(我在其中将对象添加到数组)。但我找不到问题所在,我在谷歌上搜索了同样的问题,但仍然没有运气。

任何人都可以帮助我解决我做错的问题吗?

最佳答案

你真的不应该根据一个月是 28 天甚至是 a minute is 60 seconds 的假设来计算日期。 .

代替 dateByAddingTimeInterval: 使用 NSCalendardateByAddingComponents:toDate:options:

NSDateComponents *sixMonthComponents = [[NSDateComponents alloc] init];
sixMonthComponents.month = 6;

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate* scheduleEndDate = [currentCalendar dateByAddingComponents:sixMonthComponents toDate:scheduleStartDate options:0];

编辑:据我了解,您想每隔 14 天(半个月,因为您将一个月定义为 28 天)为数组添加一个时间间隔。为了更健壮,我会做这样的事情(未经测试但应该工作,让我知道):

// We increment i with the total interval between the two dates (endMS) divided by 12 (6 months * 2 times per month)
for (double i = 0; i < endMS; i += (endMS / 12) {
[scheduleDatesArray addObject:@(i)];
}

旁注:[NSNumber numberWithDouble:i] can be written @(i) .

关于iphone - 错误 : can't allocate region in iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143480/

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