Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Memory Management: Revamped Engine Example



1.  I added a EnginePart Class and made each engine
part a subclass of the EnginePart Class.
2.  I removed the superfluous pointer.
3.  I added the NSMutableIndexSet.

Thanks Chris Hanson.

I have to get some more practice in, but I think the
NSIndexSet is going to be handy.  Is there anything
else that could be done to straighten this thing up?

Source code attached.

#import <Foundation/Foundation.h>

//EngineParts Class
@interface EngineParts : NSObject{} @end
@implementation EngineParts @end

//Individual Sub-Classes of EngineParts Class
@interface CombustionChamber : EngineParts{} @end
@implementation CombustionChamber @end

@interface Compressor : EngineParts{} @end
@implementation Compressor @end

@interface FuelInjector : EngineParts{} @end
@implementation FuelInjector @end

@interface Shaft : EngineParts{} @end
@implementation Shaft @end

@interface Turbine : EngineParts{} @end
@implementation Turbine @end


@interface Engine : NSObject
{
	 NSMutableArray *enginePartsArray;
}

- (id)init;
- (void)maintenance:(NSMutableIndexSet*)badParts;

@end


@implementation Engine

- (id)init;
{
	int i;
	
	[super init];
	 
	enginePartsArray = [[NSMutableArray alloc] init];
	 
	[enginePartsArray addObject:[[CombustionChamber
alloc] init]];
	[enginePartsArray addObject:[[Compressor alloc]
init]];
	[enginePartsArray addObject:[[FuelInjector alloc]
init]];
	[enginePartsArray addObject:[[FuelInjector alloc]
init]];
	[enginePartsArray addObject:[[Shaft alloc] init]];
	[enginePartsArray addObject:[[Turbine alloc] init]];
	
	for( i=0; i<[enginePartsArray count]; i++ )
		[[enginePartsArray objectAtIndex:i] release];
	 
	return self;
}

- (void)dealloc;
{
	[enginePartsArray release];
        
	[super dealloc];
}

- (void)maintenance:(NSMutableIndexSet*)badParts
{
	int i, part, partCount;
	 
	id PartClass;

	NSLog(@"Parts before maintenance:\n");
	for(i=0; i<[enginePartsArray count]; i++)
		NSLog(@"Part%i:  %@", i, [[enginePartsArray
objectAtIndex:i] description]);
	
	partCount = [badParts count];
	
	for( i=0; i<partCount; i++ )
	{
		part = [badParts firstIndex];
		
		PartClass = [[enginePartsArray objectAtIndex:part]
class];
		
		[enginePartsArray replaceObjectAtIndex:[badParts
firstIndex] withObject:[[PartClass alloc] init]];
		
		[[enginePartsArray objectAtIndex:part] release];
		
		[badParts removeIndex:part];
	}
        
	NSLog(@"Parts after maintenance:\n");
	for(i=0; i<[enginePartsArray count]; i++)
		NSLog(@"Part%i:  %@", i, [[enginePartsArray
objectAtIndex:i] description]);
		
		printf("\n");
}

@end

int main (int argc, const char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
	
	Engine *myEngine = [[[Engine alloc] init]
autorelease];
	 
	NSMutableIndexSet *badPartsIndexSet1 =
[[NSMutableIndexSet alloc] init];
	NSMutableIndexSet *badPartsIndexSet2 =
[[NSMutableIndexSet alloc] init];
	
	[badPartsIndexSet1 addIndex:0];
	[badPartsIndexSet1 addIndex:1];
	
	[badPartsIndexSet2 addIndex:5];
	 
	[myEngine maintenance:badPartsIndexSet1];
	[myEngine maintenance:badPartsIndexSet2];

	[badPartsIndexSet1 release];
	[badPartsIndexSet2 release];

	[pool release];
	 
     return 0;
}


	
		
__________________________________ 
Yahoo! for Good - Make a difference this year. 
http://brand.yahoo.com/cybergivingweek2005/

Attachment: MemoryEngine.m
Description: 1471405315-MemoryEngine.m

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.