• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: putting structs in NSArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: putting structs in NSArray


  • Subject: Re: putting structs in NSArray
  • From: Justin Anderson <email@hidden>
  • Date: Tue, 26 Aug 2003 14:49:14 -0400

Structs are wicked tiny memory-wise compared to NSObjects and are allocated using malloc() and free()-style functions instead of -alloc, -init, -release-style functions. Basically structs are faster and smaller but lack all the niceties of Cocoa objects.

Many C functions use structs as input and output. It's more efficient to keep that data a struct the whole time than to convert back and forth from subclassed NSObjects. Plus, if you want to save this data along with Cocoa objects to a file, an NSArray of NSValues is one way to encase a lots of structs into archivable objects.

If you instead make a separate subclass for each struct you want to use, you'll end up with lots of files with similar code. Doing it this NSValue way abstracts the struct's internals and frees you from having to subclass anything. Plus it uses almost minimal memory.

Justin Anderson

On Tuesday, August 26, 2003, at 01:22 PM, Philip D Riggs wrote:

Maybe I'm missing something, too. I understood that NSArray and
NSMutableArray could only take NSObjects. Why not make this an NSObject str
and skip the NSValue etc. stuff? Aaron Hillegass' book has an example of
adding, accessing, and deleting objects in arrays.
================================
i just can't suss this one. trying to put a struct into an array:

struct str {
int x;
char y;
BOOL z;
};
struct str s = {123456, '*', YES};

NSMutableArray *a = [[NSMutableArray alloc] initWithCapacity:2];

NSValue *v = [NSValue value:&s objCType:@encode(struct str)];

[a addObject: v];


----------------------------------------------------------------------- -----
----
Philip D. Riggs
GeoSpatial Analyst/ Laboratory Manager
Environmental Health Advanced Systems Laboratory
Colorado State University
Fort Collins, Colorado
970-491-6392
http://ehasl.cvmbs.colostate.edu/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: putting structs in NSArray (From: "Philip D Riggs" <email@hidden>)

  • Prev by Date: NSOutlineView question solved
  • Next by Date: Re: Help me, which book I should buy to learn Cocoa??
  • Previous by thread: Re: putting structs in NSArray
  • Next by thread: How to display an EPS file?
  • Index(es):
    • Date
    • Thread