My solution to the Challenge problem. NEED HELP!


EDIT: Got it!

Hey everyone!
I’ve completed the challenge and everything looks good. I’m getting the little successful indication, but for some reason I keep getting the EXC_BAD_ACCESS error. I think it has something to do with the memory management.

I am running Xcode 3.2.6 so I can not allocate the memory using @autoreleasepool. So bear with the differences in my code.

Can anyone tell me why I am getting this error?!


[code]#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

@interface StockHolding : NSObject {

float purchaseSharePrice; 
float currentSharePrice; 
int numberOfShares;


@property (copy) NSString *stockName;
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;




[code]#import “StockHolding.h”

@implementation StockHolding
@synthesize stockName,purchaseSharePrice,currentSharePrice,numberOfShares;

return purchaseSharePrice * numberOfShares;

return currentSharePrice * numberOfShares;


[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//Create three instances of StockHolding
StockHolding *stocks_one = [[StockHolding alloc] init];
StockHolding *stocks_two = [[StockHolding alloc] init];
StockHolding *stocks_three = [[StockHolding alloc] init];

//Set instance variables 
[stocks_one setStockName:@"Apple"];
[stocks_one setPurchaseSharePrice:2.3];
[stocks_one setCurrentSharePrice:4.5]; 
[stocks_one setNumberOfShares:40]; 

[stocks_two setStockName:@"Microsoft"];
[stocks_two setPurchaseSharePrice:12.19];
[stocks_two setCurrentSharePrice:10.56]; 
[stocks_two setNumberOfShares:90]; 

[stocks_three setStockName:@"Tesla"];
[stocks_three setPurchaseSharePrice:45.10];
[stocks_three setCurrentSharePrice:49.51]; 
[stocks_three setNumberOfShares:210]; 

//Create array which stores instances of StockHolding
NSMutableArray *portfolio = [NSMutableArray arrayWithObjects:stocks_one,stocks_two,stocks_three,nil]; 

//Log stocks to user
for (StockHolding *stockstouser in portfolio){
	float cost = [stockstouser costInDollars]; 
	float value = [stockstouser valueInDollars];
	NSLog(@"I own %@ shares of %@ at %@ per share. It cost %.2f. It's current price is %@ per share. The current value of %@ shares is: %.2f", [stockstouser numberOfShares],
																						  [stockstouser stockName], 
																						  [stockstouser purchaseSharePrice],
																						  [stockstouser currentSharePrice],
																						  [stockstouser numberOfShares],

[stocks_one release];
[stocks_two release];
[stocks_three release];

[pool drain];
return 0;


At the end where I have the release messages. I’m not quite sure how those work. But from what I have read, I have to release the memory from allocated objects after I am finished using there. If i’m not doing this right, please let me know.



Not sure what the issue is with your code. However I do have a question: Why are you using such an old version of xCode and what OS X version are you running?


My late 2008 macbook is running Snow Leopard (10.6.8) so the latest version of Xcode that I was able to download was 3.2.6. I would upgrade to mountain Lion, but I am anticipating an upgrade soon so I am holding of on the upgrade.


Did some digging. Turns out that if I finally pay the $99 for the ful developer account, I can download xcode 4.2 for snow leopard. Am going to end up paying up anyways, so might as well get 4.2 until I upgrade to a new MBP.