简体   繁体   English

如何循环通过CGPoints数组

[英]How to cycle through an array of CGPoints

I have created an array of 16 CGpoints representing 16 positions on a game board. 我创建了一个包含16个CGpoint的数组,它们代表游戏板上的16个位置。 This is how i set up the array CGPoint cgpointarray[16]; 这就是我设置数组CGPoint cgpointarray[16]; I would like to create a for loop to cycle through each item in the array and check if the touch is within x distance of a position (i have the position as a CGPoint. I don't have much experiance with xcode or objective c. I know the python equivalent would be 我想创建一个for循环来循环遍历数组中的每个项目,并检查触摸是否在某个位置的x距离之内(我的位置是CGPoint。我对xcode或目标c没有太多的经验。我知道python等效

 for (i in cgpointarray){
        //Stuff to do
    }

How would i accomplish this? 我将如何做到这一点? Thanks 谢谢

for (int i = 0; i < 16; i++){
        CGPoint p = cgpointarray[i];
        //do something
    }

Or if you want to use the NSArray Class: 或者,如果您想使用NSArray类:

NSMutableArray *points = [NSMutableArray array];

[points addObject:[ NSValue valueWithCGPoint:CGPointMake(1,2)]];

for(NSValue *v in points) {
       CGPoint p = v.CGPointValue;

        //do something
}

( not tested in XCode ) (未经XCode测试)

This should do it: 应该这样做:

for (NSUInteger i=0; i < sizeof(cgpointarray)/sizeof(CGPoint); i++) {
    CGPoint point = cgpointarray[i];

    // Do stuff with point
}

I would normally go for the NSValue approach above but sometimes you are working with an API where you can't change the output. 我通常会选择上面的NSValue方法,但有时您正在使用无法更改输出的API。 @Andrews approach is cool but I prefer the simplicity of .count: @Andrews的方法很酷,但我更喜欢.count的简单性:

NSArray* arrayOfStructyThings = [someAPI giveMeAnNSArrayOfStructs];
for (NSUInteger i = 0; i < arrayOfStructyThings.count; ++i) {
    SomeOldStruct tr = arrayOfStructyThings[i];
    .... do your worst here ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM