Objective-C错误缺失值

时间:2012-03-06 20:50:52

标签: objective-c nslog

这是简单的代码。它工作但缺少一个值。我发布了代码。请帮帮我。

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    //declear 3 instance varriable
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}


@property float purchaseSahrePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end

这是dot m file

#import "StockHolding.h"

@implementation StockHolding
@synthesize currentSharePrice,purchaseSahrePrice;
@synthesize numberOfShares;


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



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

@end

这是主文件

#import <Foundation/Foundation.h>
#import "StockHolding.h"

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

    @autoreleasepool {

        StockHolding *yahoo = [[StockHolding alloc] init];
        StockHolding *google =[[StockHolding alloc] init];
        StockHolding *apple = [[StockHolding alloc] init];
        //NSMutableArray *stockList = [NSMutableArray array];

        [yahoo setPurchaseSahrePrice:4.50];
        [yahoo setCurrentSharePrice:2.30];
        [yahoo setNumberOfShares:40];
      //  [stockList addObject:yahoo];

        [google setPurchaseSahrePrice:12.19];
        [google setCurrentSharePrice:10.56];
        [google setNumberOfShares:90 ];
      //  [stockList addObject:google];


        [apple setPurchaseSahrePrice:45.51];
        [apple setCurrentSharePrice:49.51];
        [apple setNumberOfShares:210];
       // [stockList addObject:apple];

        // using an array with objects
        NSMutableArray *stockList = [NSMutableArray arrayWithObjects:yahoo,google,apple, nil];


        //call the methods
        for(StockHolding *n in stockList)
        {
            //Call the methods 
            float cost = [n costInDollars];
            float value = [n valueInDollars];
            NSLog(@"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
                  [n purchaseSahrePrice],[n currentSharePrice],[n numberOfShares],cost,value);
        }


    }
    return 0;
}

以下是此代码正在运行的问题,但缺少一个值,即成本。

由于

本。

问候。

2012-03-06 20:42:59.160 Stocks[1761:707] Bought stock for $4.50, It is now at $2.30, I have 40 shares, They cost me $0.00, Now they are worth $92.00
2012-03-06 20:42:59.162 Stocks[1761:707] Bought stock for $12.19, It is now at $10.56, I have 90 shares, They cost me $0.00, Now they are worth $950.40
2012-03-06 20:42:59.163 Stocks[1761:707] Bought stock for $45.51, It is now at $49.51, I have 210 shares, They cost me $0.00, Now they are worth $10397.10

3 个答案:

答案 0 :(得分:2)

似乎是一个错字:

您调用属性setter,名为purchaseSahrePrice:[apple setPurchaseSahrePrice:45.51];

但是在获得成本的方法中,您使用名为purchaseSharePrice的实例变量。 该属性应该重命名为purchaseSharePrice,它应该可以工作。

答案 1 :(得分:2)

上面的代码中有拼写错误:@synthesis行中的purchaseSahrePrice

答案 2 :(得分:1)

你拼错了'分享':

purchaseSahrePrice

你也有内存泄漏;你需要autorelease你分配的对象。