作成した 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;
}