Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest you stick to the <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html" rel="nofollow"><code>NSValue</code></a> route, but if you really do wish to store plain 'ol <code>struct</code> datatypes in your NSArray (and other collection objects in Cocoa), you can do so -- albeit indirectly, using Core Foundation and <a href="http://www.cocoadev.com/index.pl?TollFreeBridging" rel="nofollow">toll-free bridging</a>.</p> <p><a href="http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFArrayRef/Reference/reference.html" rel="nofollow"><code>CFArrayRef</code></a> (and its mutable counterpart, <code>CFMutableArrayRef</code>) afford the developer more flexibility when creating an array object. See the fourth argument of the designated initialiser:</p> <pre><code>CFArrayRef CFArrayCreate ( CFAllocatorRef allocator, const void **values, CFIndex numValues, const CFArrayCallBacks *callBacks ); </code></pre> <p>This allows you to request that the <code>CFArrayRef</code> object use Core Foundation's memory management routines, none at all or even <em>your own</em> memory management routines.</p> <p>Obligatory example:</p> <pre><code>// One would pass &amp;kCFTypeArrayCallBacks (in lieu of NULL) if using CF types. CFMutableArrayRef arrayRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); NSMutableArray *array = (NSMutableArray *)arrayRef; struct {int member;} myStruct = {.member = 42}; // Casting to "id" to avoid compiler warning [array addObject:(id)&amp;myStruct]; // Hurray! struct {int member;} *mySameStruct = [array objectAtIndex:0]; </code></pre> <p>The above example completely ignores the issues with respect to memory management. The structure <code>myStruct</code> is created on the stack and hence is destroyed when the function ends -- the array will contain a pointer to an object that is no longer there. You can work around this by using your own memory management routines -- hence why the option is provided to you -- but then you have to do the hard work of reference counting, allocating memory, deallocating it and so on.</p> <p>I would not recommend this solution, but will keep it here in case it is of interest to anyone else. :-)</p> <hr> <p>Using your structure as allocated on the heap (in lieu of the stack) is demonstrated here:</p> <pre><code>typedef struct { float w, x, y, z; } Megapoint; // One would pass &amp;kCFTypeArrayCallBacks (in lieu of NULL) if using CF types. CFMutableArrayRef arrayRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); NSMutableArray *array = (NSMutableArray *)arrayRef; Megapoint *myPoint = malloc(sizeof(Megapoint); myPoint-&gt;w = 42.0f; // set ivars as desired.. // Casting to "id" to avoid compiler warning [array addObject:(id)myPoint]; // Hurray! Megapoint *mySamePoint = [array objectAtIndex:0]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload