سؤال

Is there a built-in class in iOS, which could be used to represent a position in a grid?

I.e. something like CGPoint, but with integer members instead of floats.

I've searched in CGGeometry Reference and other docs, but couldn't find such a class yet (it is difficult to search with such keywords).

I know I can create my custom class myself, but I'm hoping for a useful "builtin" class with nice methods.

I need it for a word game to represent tile positions:

app screenshot

هل كانت مفيدة؟

المحلول

As Martin R points out CGPoint is a struct, not a class. That being said, there is no built in method for this. You could use CGPoint an just cast accordingly throughout your code, or you can define your own struct like this:

struct CGIntegerPoint {
    NSInteger xPos;
    NSInteger yPos;
};

typedef struct CGIntegerPoint CGIntegerPoint;

CG_INLINE CGIntegerPoint CGIntegerPointMake(NSInteger x, NSInteger y) {
    CGIntegerPoint point;
    point.xPos = x;
    point.yPos = y;
    return point;
}

This now gives you a new declared type CGIntegerPoint, and a corresponding static inline function CGIntegerPointMake() which takes two arguments (x,y) and returns a CGIntegerPoint.

Note: In the future, you may find it helpful to command-click on types in Xcode. Doing so will jump you to the type's definition, which in this case, is also it's implementation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top