V
V
Valery Mishurinsky2014-02-25 21:13:09
Objective-C
Valery Mishurinsky, 2014-02-25 21:13:09

Why are the variables in the generated class not shown in main()?

Spoilers:

StackHolding.h
#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
StackHolding.m
#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

- (float)valueInDollars
{
    return currentSharePrice * numberOfShares;
}
@end
main.m
#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;
}

Xcode does not see variables from another class, then did not continue to write code. The same thing happened after I entered the example from the textbook.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rafuck, 2014-02-25
@rafuck

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

S
s0L, 2014-02-26
@s0L

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 question

Ask a Question

731 491 924 answers to any question