Challenge Error Question - Please Help!


#1

This is my attempt at the challenge. I got it right except that I wanted the stock name to populate as well and this is the error I’m getting:

2012-05-16 16:37:12.265 Stocks[1820:403] -[StockHolding nameOfStock:]: unrecognized selector sent to instance 0x107914290
2012-05-16 16:37:12.273 Stocks[1820:403] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[StockHolding nameOfStock:]: unrecognized selector sent to instance 0x107914290’
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8c217fc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8a2d5d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8c2a42ae -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8c204e73 forwarding + 371
4 CoreFoundation 0x00007fff8c204c88 _CF_forwarding_prep_0 + 232
5 Stocks 0x0000000107846393 main + 403
6 Stocks 0x00000001078461f4 start + 52
7 ??? 0x0000000000000001 0x0 + 1
)
terminate called throwing an exception(lldb)

My StockHolding.h is below:

[code]//
// StockHolding.h
// Stocks
//
// Created by Stan Cromlish - Email on 5/16/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
NSString *nameOfStock;
}
@property float costInDollars;
@property float valueInDollars;
@property int numberOfShares;

// Instance Variables that will be set.

-(float) costInDollars;
-(void) setCostInDollars:(float)c;
-(float) valueInDollars;
-(void) setValueInDollars:(float)v;
-(int) numberOfShares;
-(void) setNumberOfShares:(int)n;
-(NSString *)nameOfStock;
-(void) nameOfStock:(NSString *)s;

//The Value Method

-(float) totalStockValue;
-(float) totalStockCost;

@end
[/code]

My StockHolding.m is below:

[code]//
// StockHolding.m
// Stocks
//
// Created by Stan Cromlish - Email on 5/16/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “StockHolding.h”

@implementation StockHolding

@synthesize costInDollars;
@synthesize valueInDollars;
@synthesize numberOfShares;

-(float) costInDollars{
return costInDollars;
}

-(void)setCostInDollars:(float)c{
costInDollars = c;
}

-(float) valueInDollars{
return valueInDollars;
}

-(void) setValueInDollars:(float)v{
valueInDollars = v;
}

-(int) numberOfShares{
return numberOfShares;
}

-(void) setNumberOfShares:(int)n{
numberOfShares = n;
}

-(float) totalStockValue{
return numberOfShares * valueInDollars;
}

-(float) totalStockCost{
return numberOfShares * costInDollars;
}

-(NSString *) nameOfStock; {
return nameOfStock;
}

@end
[/code]

Here is my main.m file below:

[code]//
// main.m
// Stocks
//
// Created by Stan Cromlish - Email on 5/16/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
		

			
		// Create an instance
	StockHolding *ibm = [[StockHolding alloc] init];
	StockHolding *apl = [[StockHolding alloc] init];
	StockHolding *google = [[StockHolding alloc] init];
	
	
		//Set Stock Values
	
	[ibm nameOfStock:(@"IBM")];
	[ibm setNumberOfShares:10];
	[ibm setValueInDollars:20.75];
	[ibm setCostInDollars:11.75];
	[ibm totalStockValue];
	[ibm totalStockCost];
	
	
        [apl nameOfStock:(@"Apple")];
	[apl setNumberOfShares:12];
	[apl setValueInDollars:310.52];
	[apl setCostInDollars:127.22];
	[apl totalStockValue];
	[apl totalStockCost];

	
	[google nameOfStock:(@"Google")];
	[google setNumberOfShares:22];
	[google setValueInDollars:177.62];
	[google setCostInDollars:109.96];
	[google totalStockValue];
	[google totalStockCost];
	
	
	
		//Create a Mutable Array
	NSMutableArray *stocks = [[NSMutableArray alloc] init];
	
	[stocks addObject:ibm];
	[stocks addObject:apl];
	[stocks addObject:google];
	
		//print values in the array
	for(StockHolding *n in stocks)
	{
		
		NSLog(@"Stock %@ :Number of shares = %i Purchase price = %.2f Current price = %.2f",[n nameOfStock],[n numberOfShares]
			  ,[n costInDollars],[n valueInDollars]);
		
		NSLog(@"The cost in dollars is %.2f",[n totalStockCost]);
		NSLog(@"The value in dollars is %.2f",[n totalStockValue]);
		
	}
}
return 0;

}[/code]

What am I doing wrong? It seems so straight forward. Any help would be greatly appreciated. Thanks

Stan


#2

@synthesize nameOfStock;
creates the method setNameOfStock:, not nameOfStock:.

So this line is wrong:

[ibm nameOfStock:(@"IBM")];
it should be

[ibm setNameOfStock:@"IBM"];


#3

Thanks Aaron, that worked. I knew it would be something simple, but I just couldn’t get it figured out. I am really enjoying the book, and it is teaching me a lot. I’ve never done any programming before other than web site HTML and very little PHP. So, this is the first time that I’ve tackled trying to learn a programming from the ground up. Your book has made it easy to understand and I can take it short little bites that don’t cause my eyes to glaze over. Thanks again for a learning tool that actually was worth the money spent. Keep up the good work and I appreciate the help.