Search code examples
iosobjective-cnsarraytouchesbegan

how to access array element within touchEnded in objective c


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


Solution

  • 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.