• 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: Newbie OOP style question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie OOP style question


  • Subject: Re: Newbie OOP style question
  • From: Derrick Bass <email@hidden>
  • Date: Thu, 11 May 2006 19:04:18 -0500

On May 11, 2006, at 6:28 PM, Tim Gray wrote:

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

You should declare them as local variables inside the methods that need them.

@interface FileObject: NSObject
{

	NSNumber *fileLength;
	NSData *dataBuffer;
	unsigned long numBuffer;
}

Get rid of dataBuffer and numBuffer here
@implementation FileObject
-(void) setFileLengthWithFile: (NSFileHandle *) fileHandle
{

Add:
NSData *dataBuffer;
unsigned long numBuffer;
These two variables are local to setFileLengthWithFile. You won't be able to see them in any other scope. A scope is some code surrounded by braces { } (that's not 100% accurate, but nevermind). In C/ Objective-C you can declare local variables at the beginning of any scope. In C++/Objective-C++ you can also declare them in the middle of a scope. In either case, they vanish at the end of the scope. Note that for objective-C classes (and anything else accessed by a pointer) that doesn't mean the object pointed to disappears, although for autoreleased objects you can usually pretend that's the case.


	dataBuffer = [fileHandle readDataOfLength:4];
	[dataBuffer getBytes: &numBuffer];
	numBuffer = EndianU32_LtoN(numBuffer);
	fileLength = [NSNumber numberWithUnsignedLong: numBuffer];

There's also a memory management bug here. I assume you want to keep fileLength around for other class methods to access? If so you either need to retain it or else use an alloc/init pair. The way you have it now, it is autoreleased and so will disappear next time through the run loop.

Derrick

_______________________________________________
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


References: 
 >Newbie OOP style question (From: "Tim Gray" <email@hidden>)

  • Prev by Date: Re: Newbie OOP style question
  • Next by Date: Re: Newbie OOP style question
  • Previous by thread: Re: Newbie OOP style question
  • Next by thread: Re: Newbie OOP style question
  • Index(es):
    • Date
    • Thread