• 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
NSData confusion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSData confusion


  • Subject: NSData confusion
  • From: James Maxwell <email@hidden>
  • Date: Fri, 20 Mar 2009 11:33:32 -0700

I've been using NSData to wrap up float arrays and matrices, so I can pass them around my methods and classes.
However, I'm finding they're using loads of memory. Now, I do admit this is probably because I'm not doing this properly, so I'd like some clarification. The NSData objects are instance variables, and I need to be able to perform operations on the float arrays/matrices inside, and have these operations permanently change the state of the array/ matrix.


I've been doing something like:

- (NSData *) doSomethingToFloatData
{
    float stuff[20];

    [[self floatData] getBytes:stuff];

    // do something to change the float values in stuff

[self setFloatData:[NSData dataWithBytes:stuff length:(20 * sizeof(float))]];

    return [self floatData];
}

But this seems like a memory hungry way of doing things (even though setFloatData is a "properly written" accessor, I think, and does release the current copy "m_floatData" before storing the new one). Is there any way to just operate on the float array "in place", so to speak? Do I really need the "[self setFloatData]" call, if I want the changes to stuff to be retained?

Just in case there's a problem with the accessor, I'm doing something like this:

- (void) setFloatData:(NSData *) theData
{
    if(m_floatData)
        [m_floatData release];
    m_floatData = [theData retain];
}

This is how I've seen it done in the Apple docs, but maybe this isn't the best way to do things...(??)

Keep in mind, I'm from a Java background, so I may be missing some things that would be obvious to a C expert... I'm also open to a totally different way of doing all this, with the one caveat that the float arrays and matrices can get fairly large, and I'm running through lots of them (i.e., NSMutableArrays will likely be too slow and awkward). Would NSValue be more convenient?

J.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSData confusion
      • From: Boyd Collier <email@hidden>
    • Re: NSData confusion
      • From: mmalc Crawford <email@hidden>
    • Re: NSData confusion
      • From: Quincey Morris <email@hidden>
    • Re: NSData confusion
      • From: Nick Zitzmann <email@hidden>
  • Prev by Date: Is there an icon to represent "select all"?
  • Next by Date: Re: Is there an icon to represent "select all"?
  • Previous by thread: Re: Is there an icon to represent "select all"?
  • Next by thread: Re: NSData confusion
  • Index(es):
    • Date
    • Thread