Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在目标c中访问touchEnded中的数组元素_Ios_Objective C_Nsarray_Touchesbegan - Fatal编程技术网

Ios 如何在目标c中访问touchEnded中的数组元素

Ios 如何在目标c中访问touchEnded中的数组元素,ios,objective-c,nsarray,touchesbegan,Ios,Objective C,Nsarray,Touchesbegan,我有一个非常奇怪的行为,关于触摸感应法 我正在使用此语法访问数组元素 int cal=((b * (b-a-1))+4); printf("cal is %d",cal); c=[file_contents objectAtIndex:cal]; printf("message2------%d",c); 它在程序中的任何地方都可以正常工作,但是当我调用上面的代码时 touchesend功能使应用程序崩溃>> 实际上,这是我的函数,我在viewdidload中调用了两次,在

我有一个非常奇怪的行为,关于触摸感应法

我正在使用此语法访问数组元素

int cal=((b * (b-a-1))+4);
printf("cal is %d",cal);
c=[file_contents objectAtIndex:cal];          
printf("message2------%d",c);
它在程序中的任何地方都可以正常工作,但是当我调用上面的代码时
touchesend
功能使应用程序崩溃>>

实际上,这是我的函数,我在
viewdidload
中调用了两次,在
touchesend

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
中调用它时,一切都进行得很顺利;当在
touchesend
方法中调用它时,应用程序崩溃

我不知道出了什么问题

这是我接触过的方法

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

感谢您的任何想法或帮助

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

根据你的评论:

NSString*c=[文件内容对象索引:cal];这条线在哪里 我得到了错误的EXC_BAD_获得任何帮助@AKV

当您不保留对象并且对象被释放并尝试访问时,会显示错误
EXC\u BAD\u ACESS

如果使用ARC,请确保
文件内容
strong
类型


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

显示完整的代码,错误文本将有助于避免触摸code@AKV我编辑了问题并添加了代码现在我可以看到您正在调用方法
NSString*test=values\u retrieval(i,index)和'touchedEnd=[[Touchs anyObject]locationInView:touch.view];`我猜错误就在这里。使用断点到达错误的确切来源。@Stephan这不是错误,因为我的cal值总是小于数组索引,我已检查了itOk,我处理了3种不同的可能性。确保你的内存没有被释放。如果这不能解决任何问题,我们需要完整的代码。不过,感谢您一次发布一点信息!好吧,看起来我确实用这个回答了你的问题:“因为你正在使用ARC,或者你发布了它,所以不再存在了。”你提到的同一个错误>>保留问题thaks供你帮助两个答案都是正确的>>所以只有一个更抽象>>今天我完全理解>>这是你现在接受的。谢谢