0

我对 TouchesEnded 方法有一个非常奇怪的行为

我正在使用这种语法来访问数组元素

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

并且它在程序中的任何地方都可以正常工作,但是当我在函数中调用上面的这段代码时, touchesEnded它会使应用程序崩溃>>

实际上这是我的函数,我viewdidloadtouchesEnded

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

当它被调用时viewdidload一切顺利,当它被touchesEnded方法调用时,应用程序崩溃了

我不知道出了什么问题

这是我的 TouchedEnded 方法

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

任何想法或帮助表示赞赏

4

2 回答 2

1

c 被视为一个 int 并且您在 printf() 中为其分配了一个对象。

可能 cal 超出了数组的范围,或者您的数组超出了范围并且不再存在,因为您正在使用 ARC,或者您以其他方式释放了它。

试试这个:

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);
于 2013-02-21T07:01:20.020 回答
0

根据您的评论:

NSString *c=[file_contents objectAtIndex:cal]; 这是我收到 EXC_BAD_ACESS 错误的行,任何帮助@AKV

EXC_BAD_ACESS当您不保留对象并将其释放并尝试访问它时,将显示错误。

如果您使用的是 ARC,请确保file_contentsstrong类型。

如果您使用的是非 ARC,请使用retain.

于 2013-02-21T08:28:02.813 回答