Newbie OOP style question
Newbie OOP style question
- Subject: Newbie OOP style question
- From: "Tim Gray" <email@hidden>
- Date: Thu, 11 May 2006 19:28:32 -0400
Hi, I've just started programming in Obj-C (after a decent amount of
experience in Python) and I've got 2 questions. I'm writing a simple
CLI program that uses NSFilehandle to read in data from a file (in
little endian) and stores it in some variables. Simple enough. I'm
creating a class for these files where I declare said variables. The
plan is to have in instance for each file that is read and instance
variables for the particular attributes that I have for the files.
My question is this: I declare some temporary variables in my class
interface which I use to manipulate the info read from the file into
my Foundation data types (NSString, NSNumber, etc.). Once the info is
stored in the Foundation data types, I don't really need the temporary
variables anymore - are these something I can clear out somehow, or do
I not need to worry about it? Is this also the appropriate way to do
things? I've read through "Programming in Obj-C" by Kochan twice and
am a little freaked out by memory management (I am coming from Python
after all - declaring variables and memory management are strange to
me...).
For example, at certain part in the this file format, I need to read a
4 byte little endian number in and store the value in an NSNumber.
Here's the code:
@interface FileObject: NSObject
{
NSNumber *fileLength;
NSData *dataBuffer;
unsigned long numBuffer;
}
-(void) setFileLengthWithFile: (NSFileHandle *) fileHandle;
@end
@implementation FileObject
-(void) setFileLengthWithFile: (NSFileHandle *) fileHandle
{
dataBuffer = [fileHandle readDataOfLength:4];
[dataBuffer getBytes: &numBuffer];
numBuffer = EndianU32_LtoN(numBuffer);
fileLength = [NSNumber numberWithUnsignedLong: numBuffer];
}
------
I do similar things for other values in the files, including reading
some data from C structs. For this, I have similarly declared a
struct with the necessary components and filled it with a
[dataBuffer getBytes: &cStructVar];
I then assign the numbers/characters/etc into the appropriate
Foundation data type. I'm have the same questions for this scenario,
but maybe there is a better way for this as well.
Hope this all makes sense and thanks for the help in advanced!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden