1

我正在遵循 Big Nerd Ranch 的 Objective-C 指南,在其中一个挑战(第 17 章)中,他要求您创建您创建的对象的 3 个实例,我已经创建了类,实现了设置器、getter 和两个实例方法,然后我创建了对象的 3 个实例并定义了所有变量。然后他要求您将 3 个对象放入一个数组中,并使用 for 循环迭代这些值。

我遇到的问题是我不知道如何在“for 循环”中执行实例方法。

这是我到目前为止所拥有的(所有值都是随机的和假设的):

StockHolding.h

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

- (float)costInDollars;
- (float)valueInDollars;

@end

StockHolding.m

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars;
{
    return purchaseSharePrice * numberOfShares;
}
- (float)valueInDollars;
{
    return currentSharePrice * numberOfShares;
}

@end

主文件

#import <Foundation/Foundation.h>
#import "StockHolding.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

    StockHolding *apple = [[StockHolding alloc] init];

        [apple setPurchaseSharePrice: 10];
        [apple setCurrentSharePrice: 50];
        [apple setNumberOfShares: 20];

    StockHolding *hmv = [[StockHolding alloc] init];

        [hmv setPurchaseSharePrice: 15];
        [hmv setCurrentSharePrice: 0];
        [hmv setNumberOfShares: 17];

    StockHolding *drpepper = [[StockHolding alloc] init];

        [drpepper setPurchaseSharePrice: 5];
        [drpepper setCurrentSharePrice: 15];
        [drpepper setNumberOfShares: 70];


        NSMutableArray *stocksList = [NSArray arrayWithObjects:apple, hmv, drpepper, nil];

        for (NSObject z in stocksList) {

            NSLog(@"Original cost: %@", [z costInDollars]);
            NSLog(@"Current value: %@", [z valueInDollars]);

        }

    }
    return 0;
}
4

2 回答 2

3

你的家庭作业很好。

不编译很难发现错误。

  • NSLog您使用 %@ 的情况下[stockObj costInDollars]返回float,所以这是一个错误。利用%f

您只需要对您的代码进行一些润色。

  1. 良好的命名约定。

  2. 使用for(StockHolding *stockObj in stocksList)代替for (NSObject z in stocksList)

    for (StockHolding *stockObj in stocksList) {
        NSLog(@"Original cost: %f", [stockObj  costInDollars]);//%f as costInDollars returns float not an object
        NSLog(@"Current value: %f", [stockObj  valueInDollars]);//same as above
    }
    
  3. 可以跳过 ivars,@property 会很好用。

  4. 新编译器不需要@synthesize。如果您使用,请尝试每行使用一个属性。

  5. 而不是将值分配为:

    StockHolding *apple = [[StockHolding alloc] init];
    [apple setPurchaseSharePrice: 10];
    [apple setCurrentSharePrice: 50];
    [apple setNumberOfShares: 20];
    

    您可以使用 :

    StockHolding *apple=[[StockHolding alloc]initWithPurchaseSharePrice:10 currentSharePrice:50 numberOfShares:20];
    

    为此,您需要创建一个方法:

    -(StockHolding *)initWithPurchaseSharePrice:(float)purchase currentSharePrice:(float)current numberOfShares(int):numbers];
    
  6. 而不是NSMutableArray *stocksList = [NSArray arrayWithObjects:apple, hmv, drpepper, nil];你可以使用:

    NSMutableArray *stocksList=@[apple, hmv, drpepper];
    

编辑:

打印stockObject:调用以下方法[self printStockObject:stockObject];

-(void)printStockObject:(StockHolding *)stockObj{
    NSLog(@"Purchase Price : %f",stockObj.purchaseSharePrice);
    NSLog(@"Current Price : %f",stockObj.currentSharePrice);
    NSLog(@"Number of Shares : %d",stockObj.numberOfShares);
}
于 2013-01-23T16:57:58.323 回答
1

您是否还知道如何在 NSLOG 中打印 stockObj 的名称?我试过 NSLog(@"%@", stockObj); 但它只返回内存地址。

覆盖-description

-(NSString *)description {
    return [NSString stringWithFormat:@"%@: purchase price: %f current price: %f, number of shares: %d", NSStringFromClass([self class]), self.purchaseSharePrice, self. currentSharePrice, self.numberOfShares];
}

如果您还想要description超级类的,请执行

[NSString stringWithFormat:@"%@: purchase price: %f current price: %f, number of shares: %d %@", NSStringFromClass([self class]), self.purchaseSharePrice, self. currentSharePrice, self.numberOfShares, [super description]];

AKV 写道,你应该像这样循环:

for (StockHolding *stockObj in stocksList) {
   NSLog(@"%@",stockObj);
}

这是对的。但是,如果您有StockHolding该列表以外的其他对象,您也可以这样做

for (id stockObj in stocksList) {
    NSLog(@"%@",stockObj);
}

因为你可以发送任何消息id——如果它被实现,它将工作,否则崩溃。注意 NSObject 和id是不一样的。


在现代版本的 Xcode 中,您不需要声明 iVar。你也不需要合成。

@interface StockHolding : NSObject

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
@property (copy) NSString *shareName;


- (float)costInDollars;
- (float)valueInDollars;

@end

_如果您不合成,则ivars名称将是带有 -prefix 的属性名称

shareName->_shareName

StockHolding *apple = [[StockHolding alloc] init];
[apple setPurchaseSharePrice: 10];
[apple setCurrentSharePrice: 50];
[apple setNumberOfShares: 20];
[apple setShareName:@"apple"];

-description可能看起来像

-(NSString *)description {
    return [NSString stringWithFormat:@"%@: %@ purchase price: %f current price: %f, number of shares: %d", NSStringFromClass([self class]),_shareName, self.purchaseSharePrice, self. currentSharePrice, self.numberOfShares];
}
于 2013-01-24T12:00:17.320 回答