NSData confusion
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