我用我创建的 3 个类实例填充了一个 NSMutableArray。现在我想迭代这个数组来获取一些变量值。我可以这样做,但无法打印我的实例名称(面包、水……)相反,我得到了他们的地址。我想这很简单,但我有点挣扎,所以如果有人知道如何......谢谢
#import <Foundation/Foundation.h>
#import "StockHolding.h";
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
StockHolding *bread = [[StockHolding alloc] init];
[bread setPurchaseSharePrice:100];
[bread setCurrentSharePrice:120];
[bread setNumberOfShares:100];
StockHolding *water = [[StockHolding alloc] init];
[water setPurchaseSharePrice:100];
[water setCurrentSharePrice:80];
[water setNumberOfShares:10];
StockHolding *tomatoes = [[StockHolding alloc] init];
[tomatoes setPurchaseSharePrice:100];
[tomatoes setCurrentSharePrice:50];
[tomatoes setNumberOfShares:1];
NSMutableArray *myStock = [NSMutableArray arrayWithObjects:bread, water, tomatoes, nil];
for (StockHolding *s in myStock)
{
NSLog(@"Here is what I paid for my %p : %f", s, [s costInDollars]);
NSLog(@"Here is what I earn for my %p : %f", s, [s valueInDollars]);
}
[pool drain];
return 0;
}