我正在遵循Big Nerd Ranch的Objective-C指南,在其中一个挑战中(第17章),他要求你创建一个你创建的对象的3个实例,我已经创建了这个类,实现了setter,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
的main.m
#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;
}
答案 0 :(得分:3)
你的家庭作业很好。
没有编译就很难找到错误。
NSLog
中您使用%@作为[stockObj costInDollars]
返回float
,因此这是一个错误。使用%f
您只需要对代码进行一些修改。
良好的命名惯例。
使用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
}
可以跳过ivars,@ property可以正常工作。
新编译器中不需要@synthesize。如果您使用,请尝试每行使用一个属性。
而不是将值分配为:
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];
您可以使用以下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);
}
答案 1 :(得分: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的现代版本中,您不需要声明iVars。你不需要合成。
@interface StockHolding : NSObject
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
@property (copy) NSString *shareName;
- (float)costInDollars;
- (float)valueInDollars;
@end
如果您不合成,则ivars名称将是具有_
- 前缀
shareName
- &gt; _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];
}