如何通过快速枚举枚举此代码中实例的值,它返回数组的地址,而且我觉得这样做有一个简短的方法。
BNRStockHolding *stockHolding = [[BNRStockHolding alloc] init];
[stockHolding setPurchasedSharePrice:2.30];
[stockHolding setCurrentSharePrice:4.50];
[stockHolding setNumberOfShares:40];
float p = [stockHolding purchasedSharePrice];
float c = [stockHolding currentSharePrice];
int n = [stockHolding numberOfShares];
float cost = [stockHolding costInDollars];
float val = [stockHolding valueInDollars];
BNRStockHolding *stockHolding1 = [[BNRStockHolding alloc] init];
[stockHolding setPurchasedSharePrice:12.19];
[stockHolding setCurrentSharePrice:10.56];
[stockHolding setNumberOfShares:90];
float p1 = [stockHolding purchasedSharePrice];
float c1 = [stockHolding currentSharePrice];
int n1 = [stockHolding numberOfShares];
float cost1 = [stockHolding costInDollars];
float val1 = [stockHolding valueInDollars];
BNRStockHolding *stockHolding2 = [[BNRStockHolding alloc] init];
[stockHolding setPurchasedSharePrice:45.10];
[stockHolding setCurrentSharePrice:49.51];
[stockHolding setNumberOfShares:210];
float p2 = [stockHolding purchasedSharePrice];
float c2 = [stockHolding currentSharePrice];
int n2 = [stockHolding numberOfShares];
float cost2 = [stockHolding costInDollars];
float val2 = [stockHolding valueInDollars];
NSMutableArray *threeInstances = [NSMutableArray arrayWithObjects:
stockHolding, stockHolding1, stockHolding2, nil];
for(BNRStockHolding *d in threeInstances) {
NSLog(@"%@", d);
}
答案 0 :(得分:3)
您是否只想打印物体?如果是这样,请尝试在BNRStockHolding类中定义description
方法。像这样:
- (NSString *)description {
return [NSString stringWithFormat: @"PurchasedSharePrice=%f, CurrentSharePrice=%f", purchasedSharePrice, currentSharePrice];
}
不是打印地址,而是打印出你在那里定义的NSString。
答案 1 :(得分:0)
如果您不想修改BNRStockHolding
课程,则看起来像这样:
for(BNRStockHolding *d in threeInstances) {
NSLog(@"Number of shares: %d", [d numberOfShares]);
NSLog(@"PurchasedSharePrice: %f, CurrentSharePrice: %f", [d
purchasedSharePrice], [d currentSharePrice]);
}