Add and extract structure from NSMutableArray

Possible duplicate:
What is the best way to put c-struct in NSArray?

I need to create an array of structures, for example:

typedef struct { int fill; BOOL busy; } MapCellST; 

How to add instances of this structure to NSMutableArray and how can I extract copies of the structure and work with my properties later?

+4
source share
3 answers

Wrap the structure in NSValue :

 MapCellSt x; NSValue* value = [NSValue value:&x withObjCType:@encode(MapCellSt)]; 

To retrieve it later:

 [value getValue:&x]; 
+13
source

Use NSMutableDictionary , not NSMutableArray .

typedef struct {...} MyStruct;

 NSMutableString* myKey = [NSMutableString stringWithString:@"MyKey"]; NSValue *myStructPtr = [NSValue value:&myStruct withObjCType:@encode( MyStruct )]; [myMutableDictionary setObject:myStructPtr forKey:myKey]; 
0
source

I cant.

Only real Obj-C objects can be added directly to NSMutableArray and friends; in order to do what you are describing, you need to create a class with the properties of the structure you want to use.

Edit: As Tamas says, you can also use NSValue; but in all likelihood, creating a class for your MapCell is the best idea.

-2
source

All Articles