我的所有属性均为零,即使我用数字和对象填充了它们,数组也为零 [英] All my properties are zero, and my array is zero, even though I have populated them with numbers and objects

查看:56
本文介绍了我的所有属性均为零,即使我用数字和对象填充了它们,数组也为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经结束本章,它要求我执行以下操作:

I am the end of my chapter and it has asked me to do the following:


创建一个名为Stocks的新Foundation命令行工具。然后创建一个名为StockHolding的
类,代表您已购买
的股票。这将是NSObject的子类。对于实例变量
,它将有两个名为PurchaseSharePrice和currentSharePrice
的浮点数,以及一个名为numberOfShares的整数。为
实例变量创建访问器方法。创建其他两个实例方法:

Create new Foundation Command Line Tool called Stocks. Then create a class called StockHolding to represent a stock that you have purchased. It will be a subclass of NSObject. For instance variables, it will have two floats named purchaseSharePrice and currentSharePrice and one int named numberOfShares. Create accessor methods for the instance variables. Create two other instance methods:


  • (float)costInDollars; // purchaseSharePrice * numberOfShares

  • (float)valueInDollars; // currentSharePrice * numberOfShares
    在main()中,用三个StockHolding实例填充数组。然后
    遍历数组,打印出每个值。

我遵循了本章所学的内容。我创建了我的终端应用程序(它只能像应该的那样打印到控制台上),并添加了一个Objective-C类,然后根据我认为适当的信息对其进行了调整。

So as such I followed what I had learned through out the chapter. I created my terminal app (which is only printing to the console like it should) and I added an Objective-C class which I then adjusted with what I thought to be the proper information.

对于我的.h文件,我用以下内容填充了它:

For my .h file I filled it with the following:

@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文件中写出实现。这是我写的:

So great, I did that, no issues as far as I could tell. I proceeded to write out my implementation in my .m file. Here is what I wrote:

@implementation StocksHolding

@synthesize purchaseSharePrices, currentSharePrices, numberOfShares;

-(float) coastOfDollars{
   return (purchaseSharePrices * numberOfShares);
}

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

@end

现在,在我继续之前我想指出的是,我看到的是一个带有'!'的黄色三角形(对不起,我不记得这个名字了)。当我单击它时,上面写着未完成的实现。我去通过查看侧边栏找到了什么,然后说:未找到方法定义'costOfDollars'。这使我感到困惑,因为我几乎确定我已定义了它。在这一点上,我只是想继续处理之前,只是看我的程序是否正确运行了一些东西,我进入了main.m文件并添加了以下代码:

Now, before I move on I would like to note that I am getting a "yellow triangle with an '!' in it" (sorry I can't remember the name off hand). When I click on it is says "Incomplete implementation. I went to find what that means by looking in the side bar and it says "Method definition 'costOfDollars' not found." That confuses me because I am almost sure I defined it. So at this point, I just wanted to move on before dealing with that, just to see if my program ran some what correctly. I went to my main.m file and add the following code:

#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;
}

在那之后,我对我在解决这个问题上所取得的成就印象深刻所有的一切都在我的脑海中,但在这里一切都炸毁了。我运行了程序,它运行正常,但是我的输出却不是我所期望的:

After that I was pretty impressed with what I had accomplished in terms of figuring this all out in my head, but here is where it all blew up. I ran the program, it ran fine, but my output was not what I was expecting:


[切换到进程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 Stocks [3063:707]我以0.000000美元的价格买入了第一笔
股票,当前价格为0.000000
2012-01-12 15:12:52.395 Stocks [3063:707]我现在的价值为
0.000000程序结束,退出代码:0

[Switching to process 3063 thread 0x0] 2012-01-12 15:12:52.389 Stocks[3063:707] There are 0 stocks which I own 2012-01-12 15:12:52.392 Stocks[3063:707] The number of shares I own in my first stock is 0 2012-01-12 15:12:52.394 Stocks[3063:707] I bought the first stock for a price of 0.000000 and the current price is 0.000000 2012-01-12 15:12:52.395 Stocks[3063:707] The value of my stock now is 0.000000 Program ended with exit code: 0

我所有的值都是零。我不明白为什么会这样,这可能意味着我不了解本章中要假定的内容(本章与创建自己的类有关)。有人可以帮我了解我在这里究竟在做什么错误吗?

All of my values are zero. I don't understand why this is, which probably means I am not understanding something I was suppose to in this chapter (the chapter was about creating my own class). Could someone help me understand what exactly I am doing incorrectly here?

推荐答案

后一个问题是您声明但未初始化您的主要课程;

The latter problem is that you declared but didn't initialize your classes in main;

StocksHolding *myFirstStock; 
StocksHolding *mySecondStock; 
StocksHolding *myThirdStock;

应该是

StocksHolding *myFirstStock = [[StocksHolding alloc] init];
StocksHolding *mySecondStock = [[StocksHolding alloc] init]; 
StocksHolding *myThirdStock = [[StocksHolding alloc] init];

代码的第一部分也有一些问题;您可能不想在.h中声明实例变量,而是在.m文件中声明它们,而无需使其可见并且也具有相同名称的属性访问器。另外,有一个注释掉的{我认为是格式问题...?

The first part of the code has some problems too; you probably don't want to declare your instance variables in the .h but instead in the .m file, no need to make them visible and also have property accessors with the same name. Also, there's a commented out { that I assume is a formatting problem...?

可能不是所有错误(不是现在可以在此处进行编译和测试),但是我敢肯定,有人会更加鹰眼纠正我:)

Probably not all errors (not where I can compile and test right now), but I'm sure someone more eagle eyed will correct me :)

这篇关于我的所有属性均为零,即使我用数字和对象填充了它们,数组也为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆