在我的主要部分没有可见的@interface

时间:2014-05-03 16:48:25

标签: objective-c

我无法弄清楚为什么我收到错误没有可见的@interface为'StockHoldings'声明了slector

我用谷歌搜索了它,然后看着我,我仍然感到困惑,有些人可能指向正确的方向。谢谢。

StockHoldings.h

#import <Foundation/Foundation.h>

@interface StockHoldings : NSObject
{
    //three instance varibles
    float purchaseSharePrices;
    float currentSharePrices;
    int numberOfShares;
}

@property float purchaseSharePrices;
@property float currentSharePrices;
@property int numbeOfShares;

-(float)costInDollars; //purchaseSharePrice * numberOfShares
-(float)valueOfShares; //currentSharePrice * numberOfShares

StockHoldings.m

#import "StockHoldings.h"

@implementation StockHoldings

@synthesize purchaseSharePrices, currentSharePrices, numbeOfShares;

-(float) costInDollars
{
    return ([self purchaseSharePrices] * [self numbeOfShares]);
}

-(float) valueOfShares
{
    return ([self currentSharePrices] * [self numbeOfShares]);
}



@end

的main.m

#import <Foundation/Foundation.h>
#import "StockHoldings.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSLog(@"Hello\n");

        StockHoldings *stock1 = [[StockHoldings alloc]init];
        [stock1 currentSharePrices:1.0];
        [stock1 purchaseSharePrices:2.0];
        [stock1 numbeOfShares:3];






    }
    return 0;
}

2 个答案:

答案 0 :(得分:4)

属性的setter方法名称为set[PropertyName],因此您的代码可能应如下所示:

StockHoldings *stock1 = [[StockHoldings alloc]init];
[stock1 setCurrentSharePrices:1.0];
[stock1 setPurchaseSharePrices:2.0];
[stock1 setNumbeOfShares:3];

根据您的喜好,您可能还想使用点语法:

StockHoldings *stock1 = [[StockHoldings alloc]init];
stock1.currentSharePrices = 1.0;
stock1.purchaseSharePrices = 2.0;
stock1.numbeOfShares = 3;

最后,您可能想要修复numbeOfShares错字(错过R代表&#34;数字&#34;)。

答案 1 :(得分:0)

您没有正确使用设置器。对于@property (weak) id data合成后,setter方法就像[self setData:@"foo_bar"]。 getter方法与[self data]类似.setter方法的默认名称为set<Property>

所以在你的情况下:

[stock1 setCurrentSharePrices:1.0];
[stock1 setPurchaseSharePrices:2.0];
[stock1 setNumbeOfShares:3];