質問

I am having a very strange behaviour about the TouchesEnded Method

I am using this syntax to access the array element

int cal=((b * (b-a-1))+4);
printf("cal is %d",cal);
c=[file_contents objectAtIndex:cal];          
printf("message2------%d",c);

and its working fine anywhere in the program but when i called this above piece of code in touchesEnded function its crashing the app>>

Actually this is my function that i am calling two times one in the viewdidload and again in the touchesEnded

int values_retrieval(int a,int b)
{
//NSLog(@"for loop variable i=%i--> the retrieve coordinates are::%@", a,[file_contents objectAtIndex:(b * (b-a-1))+4]);
printf("message111111111");
int cal=((b * (b-a-1))+4);
printf("cal is %d",cal);
NSString *c=[file_contents objectAtIndex:cal];
int val=[c intValue];
printf("message222222222------%d",val);
return val;
}

when it is called in viewdidload all is going well and when it is called in touchesEnded method the app crashed

I have no idea what is going wrong

here is my TouchedEnded method

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint     touchedEnd;

    int index=5;
    int i=3;
    printf("value of i is %d",i);
    NSString *test=values_retrieval(i, index);
    printf("val issssssssssssssssssss %d",test);

    touchedEnd = [[touches anyObject] locationInView:touch.view];
}

Any idea or help is appreciated

役に立ちましたか?

解決 2

As per your comment :

NSString *c=[file_contents objectAtIndex:cal]; this is the line where i am getting error of EXC_BAD_ACESS any help @AKV

The error EXC_BAD_ACESS is shown when you do not retain the object and it is released and tries to access it.

If you are using ARC, make sure file_contents is of strong type.

If you are using non-ARC, use retain.

他のヒント

c is being treated an int and you're assigning an object to it in printf().

Chances are cal is outside of the bounds of the array, or your array went out of Scope and no longer exists because you're using ARC, or you released it otherwise.

Try this:

int cal=((b * (b-a-1))+4);
printf("cal is %d",cal);
if(cal > file_contents.count-1) 
{
printf("Cal is too big"); 
}
else 
{
c=[file_contents objectAtIndex:cal];    
}      
NSLog(@"message2------%@", c);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top