I'm having trouble working with classes. I have to create "StockHolding" objects that are subclasses of NSObject. I create instance variables and methods. Then I create 3 iterations of the stockholdings complete with names and prices, and I load them into a mutable array. I am having difficulty fast-enumerating through the objects within the array and printing the properties (prices) of each. Problem is I am getting an error when trying to enumerate through objects and print the properties. I have tried a few different ways of solving the problem with no luck. Any ideas? I also noticed currentStock is not printing a name, but a pointer location instead. Perhaps these problems are related. Thanks in advance.
'Header'
#import <Foundation/Foundation.h>
@interface StockHolding : NSObject
{
float fPurchaseSharePrice;
float fCurrentSharePrice;
int iNumberOfShares;
}
@property float fPurchaseSharePrice;
@property float fCurrentSharePrice;
@property int iNumberOfShares;
-(float) fCostInDollars; //fPurchaseSharePrice * fNumberOfShares
-(float) fValueInDollars; //fCurrentSharePrice * fNumberOfShares
@end
'Implementation'
#import "StockHolding.h"
@implementation StockHolding
@synthesize fCurrentSharePrice, fPurchaseSharePrice, iNumberOfShares;
-(float)fCostInDollars; //fPurchaseSharePrice * iNumberOfShares
{
return (fPurchaseSharePrice * iNumberOfShares);
}
-(float)fValueInDollars; //fCurrentSharePrice * iNumberOfShares
{
return (fCurrentSharePrice * iNumberOfShares);
}
@end
'Main'
int main(int argc, const char * argv[])
{
@autoreleasepool {
StockHolding *Apple = [[StockHolding alloc] init];
[Apple setFPurchaseSharePrice:225];
[Apple setFCurrentSharePrice:300];
[Apple setINumberOfShares:50];
StockHolding *Cisco = [[StockHolding alloc] init];
[Cisco setFPurchaseSharePrice:100];
[Cisco setFCurrentSharePrice:50];
[Cisco setINumberOfShares:75];
StockHolding *WalMart = [[StockHolding alloc] init];
[WalMart setFPurchaseSharePrice:75];
[WalMart setFCurrentSharePrice:150];
[WalMart setINumberOfShares:75];
NSMutableArray *Portfolio = [NSArray arrayWithObjects: Apple, Cisco, WalMart, nil];
for (NSObject *currentStock in Portfolio){
NSLog(@"Purchase Price: %@", currentStock );
NSLog(@"Details: %f", [currentStock FPurchaseSharePrice]); // <---Error is on this line. It says "No visible @interface for NSObject declares the selector fPurchaseSharePrice"
}
}
return 0;
}