gpt4 book ai didi

ios - NSMutable 数组未在另一种方法中返回值

转载 作者:行者123 更新时间:2023-11-28 21:01:41 26 4
gpt4 key购买 nike

我已经用一种方法创建了值并将其添加到 NSMutable 数组中。下面是代码

-(void)setupSegmentButtons {

NSInteger numControllers = 7;

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;

[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];

NSMutableArray *dtDate = [@[] mutableCopy];

NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];

for (int i = 0; i<numControllers; i++) {

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];

[navigationView addSubview:button];

NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];

[dtDate addObject:dateString];

[button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];

++comps.day;
}

//****** Manage to display my array with all the dates
NSLog(@" Can get my array : %@",dtDate);

}

但是当点击按钮并调用另一个方法时,NSMutable 数组 dtDate 返回(null),为什么?

-(void)tapSegmentButtonAction:(UIButton *)button {

//Can't get the NSMutable Array here
NSLog(@" Returns Null : %@",dtDate);

NSString *txtDate = dtDate[0];
//Returns NULL
NSLog(@"txtDate=%@",txtDate);
}

最佳答案

您的方法 tapSegmentButtonAction: 访问 dtDate 没有任何声明,表明它是一个实例或全局变量。

您的方法 setupSegmentButtons 声明一个变量 dtDate 创建一个局部变量,该变量隐藏任何具有相同名称的实例或全局变量。

代替:

NSMutableArray *dtDate = [@[] mutableCopy];

你应该试试:

dtDate = [NSMutableArray new]; 

HTH

关于ios - NSMutable 数组未在另一种方法中返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48337591/

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