Answer the question
In order to leave comments, you need to log in
Why are the variables in the generated class not shown in main()?
Spoilers:
#import <Foundation/Foundation.h>
@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
- (float)costInDollars;
- (float)valueInDollars;
@end
#import "StockHolding.h"
@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;
- (float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}
- (float)valueInDollars
{
return currentSharePrice * numberOfShares;
}
@end
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *threeobjects = [NSArray arrayWithObjects:purchaseSharePrice, currentSharePrice, numberOfShares, nil];
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
To access the properties of an object, you must first create the object. In your case, you need to create an instance of the StockHolding class
What a nightmare, you would read the textbook more carefully. In your main, variables are added to the array that are not declared anywhere this time. And C types are added to the array, instead of objects, these are two.
For example, this would be correct:
StockHolding* obj = [StockHolding new];
obj.purchaseSharePrice = 1;
obj.currentSharePrice = 2;
obj.numberOfShares = 3;
NSArray* fourobjects = @[@(obj.purchaseSharePrice), @(obj.currentSharePrice),
@(obj.numberOfShares), @([obj costInDollars])];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question