14

I created a structure like this

typedef struct Node {
    NSString* Description;
    NSString* AE;
    NSString* IP;
    NSString*  Port;
} Node;

I need to create NSMutableArray of this Node structure I need to know how create object of node path it to the NSMutableArray retrieve it and read for example the port.

Abizern
  • 146,289
  • 39
  • 203
  • 257
AMH
  • 6,363
  • 27
  • 84
  • 135

3 Answers3

45

After running into this problem, I came across this thread which helped, but was more complicated than the solution I ended up with.

Basically the NSValue is the wrapper for your struct, you don't need to create a new class yourself.

// To add your struct value to a NSMutableArray    
NSValue *value = [NSValue valueWithBytes:&structValue objCType:@encode(MyStruct)];
[array addObject:value];

// To retrieve the stored value
MyStruct structValue;
NSValue *value = [array objectAtIndex:0];
[value getValue:&structValue];

I hope this answer will save the next person a bit of time.

Jimmy Luong
  • 1,109
  • 1
  • 8
  • 16
  • thanks a lot, this is my first time to use NSValue to encapsulate struct. – Chauyan Nov 21 '17 at 16:07
  • 1
    Very useful answer! Question: how would this be affected with ARC turned on? If the struct has NSString* fields, is ARC smart enough to hold on to the allocations until the struct entry is removed from the NSMutableArray? – SMGreenfield Feb 29 '20 at 19:05
13

You can actually create a custom class (since it holds only NSString pointers) with struct values as instance variables. I think it'd even make more sense.

You can also create an array of NSValues which hold these structures:

NSValue *structValue = [NSValue value:&myNode objCType:@encode(Node *)];
NSMutableArray *array = [[NSMutableArray alloc] initWithObject:structValue];

You can then retreive these structs as follows:

NSValue *structValue = [array objectAtIndex:0];
Node *myNode = (Node *)[structValue pointerValue];
// or
Node myNode = *(Node *)[structValue pointerValue];
Eimantas
  • 48,927
  • 17
  • 132
  • 168
6

You can only store Objective-C objects in an NSMutableArray.

One route you can take is to use a standard C array:

unsigned int array_length = ...;
Node** nodes = malloc(sizeof(Node *) * array_length);

Another route is to wrap the structure in an Objective-C object:

@interface NodeWrapper : NSObject {
   @public

   Node *node;
}
- (id) initWithNode:(Node *) n;
@end

@implementation NodeWrapper

- (id) initWithNode:(Node *) n {
  self = [super init];
  if(self) {
     node = n;
  }
  return self;
}

- (void) dealloc {
  free(node);
  [super dealloc];
}

@end

Then, you'd add NodeWrapper objects to your NSMutableArray like this:

Node *n = (Node *) malloc(sizeof(Node));
n->AE = @"blah";
NodeWrapper *nw = [[NodeWrapper alloc] initWithNode:n];
[myArray addObject:nw];
[nw release];

To retrieve the Node from the NodeWrapper, you'd simply do this:

Node *n = nw->node;

or

Node n = *(nw->node);
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • how to retrieve data from it or insert in it could u give me more details – AMH Jun 02 '11 at 09:47
  • Hi , I used objective c class, as u said , but while try to retrieve object nothing return Node* object=(Node*) [NodesArray objectAtIndex:k]; NSLog( object.Description ); – AMH Jun 02 '11 at 10:09