views:

207

answers:

3

I am keeping track of pairs of ints and NSUIntegers as array indexes and other things.

Is there something analogous to CGPoint that is already defined?

I'm doing graphics stuff on iPhone if it matters.

+3  A: 

There is NSIndexSet which can contain a bunch of indices and has some handy manipulation API.

For simple needs, you can just define a struct or, better yet, declare a class. I often declare a class with a set of @properties, @synthesizing all getters/setters, to encapsulate data. Easy, very little code, and it simplifies refactoring to add functionality later (i.e. if you decide your struct really wanted to be a class).

bbum
+1  A: 

CGPoint and NSPoint (note that NSPoint is not available on the iPhone) would work just fine, but you need to consider the semantic implications of using either. If you truly are dealing with vectors, than you should use those. But if you are merely dealing with pair indexes, you may want to declare your own struct or class.

If you are sure you only need a data structure (that is, something without any functionality except housing the data), declare a C struct. It is really simple and uses less memory than an Objective-C class. If you want it to have built-in functionality, though, go with a class as @bbum explained.

Jonathan Sterling
+3  A: 

It's fairly easy to define your own struct to hold the data. You can use the CGPoint struct type (of which NSPoint is pretty much a #define alias) but you really need to define what you're using it for.

typedef struct _pair {
  int first,
  int second
} pair;

You can then do:

pair foo; foo.first = 1; foo.second = 2;

Note this is only sensible if you have a fixed number of elements. If you're looking for a set of elements, you really want an array of ints.

AlBlue