Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 我的所有属性都是零,我的数组也是零,即使我已经用数字和对象填充了它们_Objective C_Arrays_Xcode - Fatal编程技术网

Objective c 我的所有属性都是零,我的数组也是零,即使我已经用数字和对象填充了它们

Objective c 我的所有属性都是零,我的数组也是零,即使我已经用数字和对象填充了它们,objective-c,arrays,xcode,Objective C,Arrays,Xcode,我是本章的结尾,它要求我做以下工作: @interface StocksHolding : NSObject //Declare instance variables { float purchaseSharePrices; float currentSharePrices; int numberOfShares; } //Decalre methods -(float) costOfDollars; // purchaseSharePrice*numberOfSh

我是本章的结尾,它要求我做以下工作:

@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
创建新的基础命令行工具,称为股票。然后创建一个 名为StockHolding的类表示您拥有的股票 购买。它将是NSObject的一个子类。比如变量,, 它将有两个浮动名为purchaseSharePrice和currentSharePrice 和一个名为numberOfShares的int。为 实例变量。创建其他两个实例方法:

  • (浮动)肋支purchaseSharePrice*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;
}
#导入
#导入“StocksHolding.h”
int main(int argc,const char*argv[]
{
@自动释放池{
StocksHolding*我的第一只股票;
StocksHolding*mySecondStock;
StocksHolding*myThirdStock;
NSArray*myStockHoldings=[NSArray数组及其对象:myFirstStock、mySecondStock、myThirdStock、nil];
//设置myFirstStock的值
[myFirstStock setNumberOfShares:(3)];
[myFirstStock setPurchaseSharePrices:(10.50)];
[myFirstStock setCurrentSharePrices:(30.86)];
//设置mySecondStock的值
[mySecondStock setNumberOfShares:(4)];
[mySecondStock setPurchaseSharePrices:(40.80)];
[mySecondStock setCurrentSharePrices:(30.96)];
//设置myThirdStock的值
[myThirdStock setNumberOfShares:(20)];
[myThirdStock setPurchaseSharePrices:(90.50)];
[myThirdStock SetCurrent股价:(108.93)];
//打印已收集的信息
NSLog(@“我拥有%的lu股票,[myStockHoldings count]);
NSLog(@“我在我的第一只股票中拥有的股票数量为%d”,[myFirstStock numberOfShares]);
NSLog(@“我以%f的价格购买了第一只股票,当前价格为%f”,[myFirstStock purchaseSharePrices],[myFirstStock costOfDollars]);
NSLog(@“我的股票现在的价值是%f”,[myFirstStock valueInDollars]);
}
返回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 我的股票现在的价值是 0.000000程序以退出代码结束:0


我所有的值都是零。我不明白这是为什么,这可能意味着我没有理解我在本章中应该理解的东西(本章是关于创建我自己的类的)。有人能帮我理解我在这里到底做了什么错误吗?

后一个问题是您声明了类,但没有在main中初始化类

StocksHolding *myFirstStock; 
StocksHolding *mySecondStock; 
StocksHolding *myThirdStock;
应该是

StocksHolding *myFirstStock = [[StocksHolding alloc] init];
StocksHolding *mySecondStock = [[StocksHolding alloc] init]; 
StocksHolding *myThirdStock = [[StocksHolding alloc] init];
代码的第一部分也有一些问题;您可能不想在.h文件中声明实例变量,而是在.m文件中声明实例变量,不需要使它们可见,也不需要使用同名的属性访问器。还有一个被注释掉的{我想这是一个格式问题


可能不是所有的错误(不是我现在可以编译和测试的地方),但我相信有人会更敏锐地纠正我:)

对于我们这些仍在寻找答案(或者至少是一种可能的方法)的人,这里是我的尝试。对我温和一点,因为我是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;
}
希望这能帮助目前正在阅读这本书的人,或者作为一个有趣(或者可能是搞笑的Obj.C)的信息点

问候,,
Eelco

“costOfDollars”与“costOfDollars”不一样。哇!你看,这样的小东西会把你的整个程序都扔掉。谢谢你!在编程方面没有“小东西”。谢谢,有人注意到我有一个拼写错误,在一个地方写了costOfDollars,在另一个地方写了costOfDollars。
//  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