Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an NSMutableArray of structs?

Tags:

objective-c

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.

like image 534
AMH Avatar asked Jun 02 '11 09:06

AMH


People also ask

Can you create an array of structs?

Create an Array of struct Using the malloc() Function in C The memory can be allocated using the malloc() function for an array of struct . This is called dynamic memory allocation. The malloc() (memory allocation) function is used to dynamically allocate a single block of memory with the specified size.

How do you create an array of structures?

To create an array of structures using the struct function, specify the field value arguments as cell arrays. Each cell array element is the value of the field in the corresponding structure array element. For code generation, corresponding fields in the structures must have the same type.

How do you create an array of structs in Objective-C?

To declare an array in Objective-C, we use the following syntax. type arrayName [ arraySize ]; type defines the data type of the array elements. type can be any valid Objective-C data type.

How do you create an array of structs in Golang?

In the Go language, you can set a struct of an array. To do so see the below code example. Where type company struct has a slice of type employee struct. Here, you can see the two different structs, and the employee struct is used as an array in the company struct.

What is the use of nsmutablearray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray. NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray.

How to create an array of structs in C++?

This article will demonstrate multiple methods of how to create an array of structs in C++. Fixed length array of structs can be declared using [] C-style array notation. In this case, we defined an arbitrary struct named Company with multiple data members and initialized 2 element array.

Is it possible to declare an array with multiple data members?

In this case, we defined an arbitrary struct named Company with multiple data members and initialized 2 element array. The only downside of this method is that the declared array is a raw object without any built-in functions.

How to declare a variable array with multiple built-in methods?

Alternatively, we can utilize a std::vector container to declare a variable array that provides multiple built-in methods for data manipulation. The std::vector object can be initialized with the same notation as the previous example.


1 Answers

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.

like image 180
Jimmy Luong Avatar answered Sep 28 '22 06:09

Jimmy Luong