我是本章的结尾,它要求我做以下事情:
创建名为Stocks的新基础命令行工具。然后创建一个 名为StockHolding的类代表您拥有的股票 购买。它将是NSObject的子类。例如变量, 它将有两个名为purchaseSharePrice和currentSharePrice的浮点数 和一个名为numberOfShares的int。为。创建访问器方法 实例变量。创建另外两个实例方法:
- (浮点)costInDollars; // purchaseSharePrice * numberOfShares
- (浮点)valueInDollars; // currentSharePrice * numberOfShares 在main()中,使用三个StockHolding实例填充一个数组。然后 遍历数组打印出每个数值。
因此,我遵循了本章所学到的内容。我创建了我的终端应用程序(它只是打印到控制台),然后我添加了一个Objective-C类,然后我调整了我认为正确的信息。
对于我的.h文件,我填写了以下内容:
@interface StocksHolding : NSObject
//Declare instance variables {
float purchaseSharePrices;
float currentSharePrices;
int numberOfShares; }
//Decalre methods
-(float) costOfDollars; // purchaseSharePrice*numberOfShares
-(float) valueInDollars; // currentSharePrices*numberOfShares
//Declare setter and getter methods
@property float purchaseSharePrices;
@property float currentSharePrices;
@property int numberOfShares;
@end
太棒了,我做到了,据我所知,没有任何问题。我继续在我的.m文件中写出我的实现。这是我写的:
@implementation StocksHolding
@synthesize purchaseSharePrices, currentSharePrices, numberOfShares;
-(float) coastOfDollars{
return (purchaseSharePrices * numberOfShares);
}
-(float) valueInDollars{
return (currentSharePrices * numberOfShares);
}
@end
现在,在我继续前进之前,我想指出我正在获得一个带有'!'的黄色三角形。在它“(对不起,我不记得手中的名字)。当我点击它时说“不完整的实现。我通过查看侧栏找到了什么意思,它说”方法定义'costOfDollars'找不到。“这让我感到困惑,因为我几乎可以肯定我已经定义了它。所以在这一点上,我只是想继续处理之前,只是为了看看我的程序是否运行了一些正确的东西。我去了我的main.m文件并添加了以下代码:
#import <Foundation/Foundation.h>
#import "StocksHolding.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
StocksHolding *myFirstStock;
StocksHolding *mySecondStock;
StocksHolding *myThirdStock;
NSArray *myStockHoldings = [NSArray arrayWithObjects:myFirstStock,mySecondStock,myThirdStock, nil];
//Set the values for myFirstStock
[myFirstStock setNumberOfShares:(3)];
[myFirstStock setPurchaseSharePrices:(10.50)];
[myFirstStock setCurrentSharePrices:(30.86)];
//Set the values for mySecondStock
[mySecondStock setNumberOfShares:(4)];
[mySecondStock setPurchaseSharePrices:(40.80)];
[mySecondStock setCurrentSharePrices:(30.96)];
//Set the values for myThirdStock
[myThirdStock setNumberOfShares:(20)];
[myThirdStock setPurchaseSharePrices:(90.50)];
[myThirdStock setCurrentSharePrices:(108.93)];
//Printing the information that has been gathered
NSLog(@"There are %lu stocks which I own", [myStockHoldings count]);
NSLog(@"The number of shares I own in my first stock is %d", [myFirstStock numberOfShares] );
NSLog(@"I bought the first stock for a price of %f and the current price is %f", [myFirstStock purchaseSharePrices], [myFirstStock costOfDollars]);
NSLog(@"The value of my stock now is %f", [myFirstStock valueInDollars]);
}
return 0;
}
在那之后,我对我在脑海中解决这个问题所取得的成就印象非常深刻,但这就是爆炸的地方。我运行程序,它运行正常,但我的输出不是我所期望的:
[切换到进程3063线程0x0] 2012-01-12 15:12:52.389 股票[3063:707]我有0支股票2012-01-12 15:12:52.392股票[3063:707]我在第一次拥有的股票数量 股票是0 2012-01-12 15:12:52.394股票[3063:707]我买了第一个 股票的价格为0.000000,当前价格为0.000000 2012-01-12 15:12:52.395股票[3063:707]我现在的股票价值是 0.000000程序以退出代码结束:0
我的所有值都是零。我不明白为什么会这样,这可能意味着我不理解我在本章中想到的东西(章节是关于创建我自己的类)。有人可以帮我理解我在这里做错了什么吗?
答案 0 :(得分:1)
后一个问题是你声明但没有在main中初始化你的类;
StocksHolding *myFirstStock;
StocksHolding *mySecondStock;
StocksHolding *myThirdStock;
应该是
StocksHolding *myFirstStock = [[StocksHolding alloc] init];
StocksHolding *mySecondStock = [[StocksHolding alloc] init];
StocksHolding *myThirdStock = [[StocksHolding alloc] init];
代码的第一部分也存在一些问题;您可能不希望在.h中声明实例变量,而是在.m文件中,不需要使它们可见,并且还具有相同名称的属性访问器。另外,有一个注释掉了{我认为是一个格式化问题......?
可能不是所有的错误(不是我现在可以编译和测试的地方),但是我确信有更多的老鹰眼睛会纠正我:)
答案 1 :(得分:0)
对于我们这些仍在寻找答案的人(或者至少有一种可能的方法),这是我的尝试。对我很温柔,因为我是Obj的新手。 C(或一般的编程,真的),虽然它似乎对我很好。请注意,列出的代码基于Hillegass的书中提出的问题:
的main.m:
// main.m
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
StockHolding *firstInstance = [[StockHolding alloc] init];
StockHolding *secondInstance = [[StockHolding alloc] init];
StockHolding *thirdInstance = [[StockHolding alloc] init];
// Create an empty mutable array
NSMutableArray *stockArray = [NSMutableArray array];
// Add instances
[stockArray addObject:firstInstance];
[stockArray addObject:secondInstance];
[stockArray addObject:thirdInstance];
[firstInstance setPurchaseSharePrice:2.3];
[firstInstance setCurrentSharePrice:4.5];
[firstInstance setNumberOfShares:40];
[secondInstance setPurchaseSharePrice:12.10];
[secondInstance setCurrentSharePrice:10.56];
[secondInstance setNumberOfShares:20];
[thirdInstance setPurchaseSharePrice:45.10];
[thirdInstance setCurrentSharePrice:48.51];
[thirdInstance setNumberOfShares:210];
for (id stock in stockArray)
{
// Calculate the (current) value of the stock using the valueInDollars method
float value = [stock valueInDollars];
// Calculate the purchase price of the stock using the costInDollars method
float cost = [stock costInDollars];
// Calculate the profit gained thus far
float profit = (value - cost);
// Return the results to the log
NSLog(@"You've got %d stocks, which costed you %.2f and are now worth %.2f. The profit for this stock thus far is %.2f", [stock numberOfShares], cost, value, profit);
}
}
return 0;
}
这是StockHolding.h文件。请注意,访问者快捷方式@property和@synthesize正在按照书中的要求使用。
//
// StockHolding.h
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
@end
最后,StockHolding.m:
// StockHolding.m
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import "StockHolding.h"
@implementation StockHolding
@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;
- (float)costInDollars
{
// Cacluate costInDollar, purchaseSharePrice * numberOfShares
return purchaseSharePrice * numberOfShares;
};
- (float)valueInDollars{
// Calcuate valueInDollars, currentSharePrice * numberOfShares
return currentSharePrice * numberOfShares;
};
@end
希望这可以帮助目前正在阅读本书的任何人,或者作为一个有趣的(或可能是对Obj.C的滑稽尝试)信息。
此致 Eelco