【问题标题】:Printing array object at index:i brings up compiler error在索引处打印数组对象:i 会引发编译器错误
【发布时间】:2023-03-21 12:52:02
【问题描述】:

这里有点混乱。

我将一个类中的一些对象添加到一个数组中。现在我正在尝试遍历这些对象并将它们与所有变量一起打印(到控制台)。

NSArray *stockArray=[NSArray arrayWithObjects:stock1, stock2, stock3, nil];

    for (int i=0; i<4; i++)
    {
        StockHolding *stockItem=[[StockHolding alloc]init];
        stockItem=[stockArray objectAtIndex:i];
        [print stockItem];
    }

我的 Stock Holding 类有几个属性,我在 .h 中声明并在 .m 中与 print 方法一起综合。

但是,当我尝试在上面的代码中使用它来打印“stockItem”时,我收到编译器错误“Use of undeclared identifier 'print'”

这没有意义,因为 Stock Holding 类在 .h 中声明 print 并在 .m 中实现:

-(void) print{
NSLog(@"Current purchase price is %f, current price is %f, 
number of shares are %i, cost in dollars is %f, value is dollars is %f", 
purchaseSharePrice, currentSharePrice, 
numberOfShares, self.costInDollars, self.valueInDollars);
}

我应该使用其他方式来打印这些数组对象吗?

【问题讨论】:

    标签: objective-c arrays printing


    【解决方案1】:

    你的语法是倒退的。应该是:

    [stockItem print];
    

    Objective-C 语法如下:

    [object method];
    

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多