-3

我想从我的 NSMutableArray 中获取数据,我正在这样做

-(void)ViewDidLoad{
     A_array = [[NSMutableArray alloc] init];
     A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]
    var_Count_Answer_A   = 0;    // int  
}

-(void)method_Second {
    NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);    // This line is crashing if I  click button again. First time line works fine but if we click button again and    var_Count_Answer = 2 then it will crash.

    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]];   // If I comment on NSLOG then this line will crash
    A = [str1 integerValue];
    NSLog(@"A is %d",A);


    var_Count_Answer_A ++;
}

如果我试试这个NSLog(@"%@",[A_array objectAtIndex:5]); // works fine

NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);  // var_Count_Answer_A =5; and crash

任何想法或建议将不胜感激。

4

4 回答 4

3

你的代码伤害了我的眼睛

在 Objective-C 中编写代码时需要遵循某些编码标准。每种语言都有一个。它们使代码具有可读性和可理解性。您可以参考Cocoa 编码指南并帮助自己编写更好看的代码。

正确解释

如果没有崩溃前的错误消息,没有人可以帮助您。下次当您发布有关崩溃的信息时,还要解释您收到的错误消息。

EXEC_BAD_ACCESS吗?如果是这样,那就是令人敬畏的垃圾收集器将您的数组对象扫走。无论如何,你的代码让我眼花缭乱,我正在接受重构代码的特权。也许它会帮助你。

一点重构

NSArray *aArray;
- (void)viewDidLoad
   {
     [super viewDidLoad];
     aArray = @[@"1", @"2", @"3", @"4"];
     // or use[[NSArray alloc]initWithObjects:@"1", @"2", @"3", @"4", @"5"];
     //Why do you need a mutable array while initializing it static?
   }

 - (void)methodSecond
   {
     static int counter = 0;
       if (aArray) {
         if (counter<aArray.count) {
           NSLog(@"A is %u",[[aArray objectAtIndex:counter++]integerValue]);
         } else {
           counter = 0;
           [self methodSecond];
         }
       }
   }

了解你在做什么

所以我猜你的问题的详细解释是:

  • 您正在使用 [NSArray initWithObjects:] 这会创建一个自动释放的对象,我们无法控制它的生命周期。如果在创建后没有立即引用它们,它们就会被释放,这可能就是你的情况。以我的经验,可变的自动释放对象总是会导致访问问题。所以最好让对象分配和初始化,即.. [[NSArray alloc]initWithObjects:] 我观察到你没有在运行时添加/删除数组成员。没有 MutableArray 的目的。

  • 在您的代码中,您正在创建两个对象。第一个数组对象被分配并在下一行立即取消引用。这可能是一个错误。

如果您想了解有关自动释放对象的更多信息。读这个

于 2013-07-17T08:00:40.437 回答
1

Use

A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];

Instead you used.

A_array = [[NSMutableArray alloc] init];
     A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];

As I review Your Project is ARC disable try this Working fine for me.

ViewControler.h
@interface MasterViewController : UIViewController 
{
    NSMutableArray *A_array;
    int var_Count_Answer_A;
}

ViewControler.m
-(void)viewDidLoad  {
     [super viewDidLoad];
     A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];
var_Count_Answer_A   = 0;
     var_Count_Answer_A   = 0;    // int
        }

-(IBAction)method_Second:(id)sender {
     NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);    

     NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]];   // If I comment on NSLOG then this line will crash
     int A = [str1 integerValue];
     NSLog(@"A is %d",A);

     var_Count_Answer_A++;
        }

But As Everyone know at last there must be occur this exception.

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 16 beyond bounds [0 .. 15]'

As trying to access array index object that does not exist. And if you still have some error then First Enable NSZombie in your project Follow this.

-> "Product" menu, select "Edit Scheme", go to the "YourAp.app" stage in the left panel, and the "Arguments" tab on the right. You can then add NSZombieEnabled to the "Environment Variables" section and set the value to YES.

And Now Share your Crash Log.

于 2013-07-17T07:14:21.077 回答
0

在您的代码中,一旦检查了这两个变量,您就会使用两个不同的变量。

var_Count_Answervar_Count_Answer_A

于 2013-07-17T06:59:29.547 回答
0

发生此错误是因为您使用的是自动释放的对象。当你打电话时:

A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]

A_array 是一个自动释放的对象,你不能说它什么时候会被释放。这就是发生此错误的原因。

解决方案:

为数组声明一个属性,例如:

@property (nonatomic, strong) NSMutableArray *A_array;

然后修改您的方法,例如:

-(void)ViewDidLoad
{
     _A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]
    var_Count_Answer_A   = 0;    // int  
}

-(void)method_Second
{
    NSLog(@"%@",[_A_array objectAtIndex:var_Count_Answer_A])
    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[_A_array objectAtIndex:var_Count_Answer_A]];
    A = [str1 integerValue];
    NSLog(@"A is %d",A);
    var_Count_Answer_A ++;
}
于 2013-07-17T09:09:32.770 回答