递增时 NSInteger 的疯狂值
NSInteger crazy value when incrementing
我一直在寻找这个问题有一段时间了,所以我决定问一下。我真的不知道发生了什么。
我有一个 tableView,当我 select 一个单元格时,我希望它转到不同的视图控制器。到目前为止一切都很好,问题是我不想继续添加屏幕,所以我试图使用一个计数器来检查我是否已经使用过一次正常的 segue,以便我可以使用 unwind 方法。考虑到这一点,这是我的代码片段
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger antcounter;
if (indexPath.row == 0 && indexPath.section == 0 && antcounter < 2) {
[self performSegueWithIdentifier:@"ToAntMan" sender:self];
antcounter ++;
NSLog(@"%tu", antcounter);
} else if (indexPath.row == 0 && indexPath.section == 0 && antcounter > 2) {
NSLog(@"%tu", antcounter);
[self performSegueWithIdentifier:@"unwindToAntMan" sender:self];
NSLog(@"YES");
在第一个 NSLog 中,我得到的值为 2
在第二个 NSLog 中,我得到了一个疯狂的值 1732,我真的无法理解。我在做什么傻事?
没有初始化的 NSInteger 不能保证为零,因为它是一个局部自动变量,它的值是不确定的。如果你希望它为零。
或者
NSUInteger antcounter = 0;
或
static NSUInteger antcounter;
除此之外,您可以在头文件中将其声明为全局,如下所示
@property (nonatomic, assign) NSInteger antcounter;
希望对您有所帮助。
我一直在寻找这个问题有一段时间了,所以我决定问一下。我真的不知道发生了什么。
我有一个 tableView,当我 select 一个单元格时,我希望它转到不同的视图控制器。到目前为止一切都很好,问题是我不想继续添加屏幕,所以我试图使用一个计数器来检查我是否已经使用过一次正常的 segue,以便我可以使用 unwind 方法。考虑到这一点,这是我的代码片段
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger antcounter;
if (indexPath.row == 0 && indexPath.section == 0 && antcounter < 2) {
[self performSegueWithIdentifier:@"ToAntMan" sender:self];
antcounter ++;
NSLog(@"%tu", antcounter);
} else if (indexPath.row == 0 && indexPath.section == 0 && antcounter > 2) {
NSLog(@"%tu", antcounter);
[self performSegueWithIdentifier:@"unwindToAntMan" sender:self];
NSLog(@"YES");
在第一个 NSLog 中,我得到的值为 2 在第二个 NSLog 中,我得到了一个疯狂的值 1732,我真的无法理解。我在做什么傻事?
没有初始化的 NSInteger 不能保证为零,因为它是一个局部自动变量,它的值是不确定的。如果你希望它为零。
或者
NSUInteger antcounter = 0;
或
static NSUInteger antcounter;
除此之外,您可以在头文件中将其声明为全局,如下所示
@property (nonatomic, assign) NSInteger antcounter;
希望对您有所帮助。