-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 でコードを記述する際には、従うべき特定のコーディング標準があります。すべての言語に 1 つありました。それらは、コードを読みやすく理解しやすいものにします。Cocoa Coding Guidelinesを参照して、見栄えの良いコードを作成してください。

きちんと説明する

クラッシュする前にエラーメッセージがなければ、誰もあなたを助けることができません. 次回、クラッシュについて投稿するときは、表示されたエラー メッセージについても説明してください。

でした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 を持つ目的はありません。

  • コードでは、2 つのオブジェクトを作成しています。最初の配列オブジェクトが割り当てられ、次の行ですぐに逆参照されます。これはおそらく間違いです。

自動解放されたオブジェクトについて詳しく知りたい場合。これを読んでください。

于 2013-07-17T08:00:40.437 に答える
1

使用する

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

代わりに使用しました。

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];

Your Project is ARC disable を確認すると、この 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++;
        }

しかし、誰もが知っているように、ついにこの例外が発生するに違いありません。

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

存在しない配列インデックス オブジェクトにアクセスしようとしている。 And if you still have some error then First Enable NSZombie in your project Follow this.

-> " Product" メニューから " Edit Scheme" を選択し、左側のパネルの " YourAp.app" ステージに移動しArguments、右側の " " タブに移動します。NSZombieEnabled次に、「 」セクションに追加してEnvironment Variables、値を に設定できますYES

And Now Share your Crash Log.

于 2013-07-17T07:14:21.077 に答える
0

あなたのコードでは、2つの異なる変数を使用しています.2つをチェックしてください。

var_Count_Answervar_Count_Answer_A

于 2013-07-17T06:59:29.547 に答える