Aaron Hillegass (Exercise of Part 17).
Aaron Hillegass(第17部分的练习)。
Problem: In my loop, I would like it to log the values for each of the 3 StockHolding
objects, but right now it's only logging the values for DHL
. How can I get my loop to log the values for the other 2 objects?
问题:在我的循环中,我希望它记录3个StockHolding对象中每个对象的值,但是现在它只记录DHL的值。如何让循环记录其他2个对象的值?
Thank you!
谢谢!
P.S. StockHolding.h и StockHolding.m are all right.
附: StockHolding.h和StockHolding.m都没问题。
import <Foundation/Foundation.h>
import "StockHolding.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
StockHolding *DHL = [[StockHolding alloc] init];
[DHL setPurchaseSharePrice:345.67];
[DHL setNumberOfShares:23];
[DHL setCurrentSharePrice:389.23];
StockHolding *Sony = [[StockHolding alloc] init];
[Sony setPurchaseSharePrice:587.12];
[Sony setNumberOfShares:14];
[Sony setCurrentSharePrice:603.81];
StockHolding *EPAM = [[StockHolding alloc] init];
[EPAM setPurchaseSharePrice:178.45];
[EPAM setNumberOfShares:35];
[EPAM setCurrentSharePrice:190.64];
NSMutableArray *result = [[NSMutableArray alloc] initWithObjects:DHL, Sony, EPAM, nil];
for (NSObject *n in result) {
NSLog(@"Purchase for: %f\nNumber of shares: %d\nCurrent share price: %f\nCost,$: %f\nValue,$: %f\n", DHL.purchaseSharePrice, DHL.numberOfShares, DHL.currentSharePrice, DHL.costInDollars, DHL.valueInDollars);
}
}
return 0;
}
Problem solved:
问题解决了:
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
StockHolding *DHL = [[StockHolding alloc] init];
[DHL setNameOfShare:@"DHL"];
[DHL setPurchaseSharePrice:345.67];
[DHL setNumberOfShares:23];
[DHL setCurrentSharePrice:389.23];
StockHolding *Sony = [[StockHolding alloc] init];
[Sony setNameOfShare:@"Sony"];
[Sony setPurchaseSharePrice:587.12];
[Sony setNumberOfShares:14];
[Sony setCurrentSharePrice:603.81];
StockHolding *EPAM = [[StockHolding alloc] init];
[EPAM setNameOfShare:@"EPAM"];
[EPAM setPurchaseSharePrice:178.45];
[EPAM setNumberOfShares:35];
[EPAM setCurrentSharePrice:190.64];
NSMutableArray *result = [[NSMutableArray alloc] initWithObjects:DHL, Sony, EPAM, nil];
for (StockHolding *n in result) {
NSLog(@"\nName: %@\nPurchase for: %.2f\nNumber of shares: %lu\nCurrent share price: %.2f\nCost,$: %.2f\nValue,$: %.2f\n", n.nameOfShare, n.purchaseSharePrice, n.numberOfShares, n.currentSharePrice, n.costInDollars, n.valueInDollars);
}
}
return 0;
}
There is another option:
还有另一种选择:
[result enumerateObjectsUsingBlock:^(StockHolding *DHL, NSUInteger idx, BOOL *stop) {
NSLog(@"\nName: %@\nPurchase for: %.2fl\nNumber of shares: %lu\nCurrent share price: %.2fl\nCost,$: %.2fl\nValue,$: %.2fl\n", DHL.nameOfShare, DHL.purchaseSharePrice, (unsigned long)DHL.numberOfShares, DHL.currentSharePrice, DHL.costInDollars, DHL.valueInDollars);
}];
Thanks you all!
谢谢大家!
1 个解决方案
#1
1
I'm not sure what you're trying to do, but it looks like the for loop at the end of your code is wrong. I imagine you're trying to run the NSLog for each of the three StockHolding
variables? The way you have it written, it's just going to repeat the values for DHL 3 times.
我不确定你要做什么,但看起来代码末尾的for循环是错误的。我想你正试图为三个StockHolding变量中的每一个运行NSLog?你写它的方式,它只是重复DHL的值3次。
Try this instead:
试试这个:
for (StockHolding *n in result) {
NSLog(@"Purchase for: %f\nNumber of shares: %d\nCurrent share price: %f\nCost,$: %f\nValue,$: %f\n",
n.purchaseSharePrice,
n.numberOfShares,
n.currentSharePrice,
n.costInDollars,
n.valueInDollars);
}
#1
1
I'm not sure what you're trying to do, but it looks like the for loop at the end of your code is wrong. I imagine you're trying to run the NSLog for each of the three StockHolding
variables? The way you have it written, it's just going to repeat the values for DHL 3 times.
我不确定你要做什么,但看起来代码末尾的for循环是错误的。我想你正试图为三个StockHolding变量中的每一个运行NSLog?你写它的方式,它只是重复DHL的值3次。
Try this instead:
试试这个:
for (StockHolding *n in result) {
NSLog(@"Purchase for: %f\nNumber of shares: %d\nCurrent share price: %f\nCost,$: %f\nValue,$: %f\n",
n.purchaseSharePrice,
n.numberOfShares,
n.currentSharePrice,
n.costInDollars,
n.valueInDollars);
}