2012-08-27 33 views
3

我在使用類時遇到了問題。我必須創建NSObject的子類的「StockHolding」對象。我創建實例變量和方法。然後我創建了3個完整的名稱和價格的庫存代碼,並將它們加載到一個可變數組中。我無法快速枚舉數組中的對象並打印每個對象的屬性(價格)。問題是我試圖通過對象枚舉並打印屬性時出現錯誤。我嘗試了幾種不同的方法來解決問題,但沒有運氣。有任何想法嗎?我還注意到當前的庫存不是打印一個名字,而是一個指針位置。也許這些問題是相關的。提前致謝。大書呆子牧場目標C第17章挑戰 - 定義類

'頭'

#import <Foundation/Foundation.h> 

@interface StockHolding : NSObject 
{ 
    float fPurchaseSharePrice; 
    float fCurrentSharePrice; 
    int iNumberOfShares; 
} 

@property float fPurchaseSharePrice; 
@property float fCurrentSharePrice; 
@property int iNumberOfShares; 

-(float) fCostInDollars; //fPurchaseSharePrice * fNumberOfShares 
-(float) fValueInDollars; //fCurrentSharePrice * fNumberOfShares 

@end 

'實現'

#import "StockHolding.h" 

@implementation StockHolding 

@synthesize fCurrentSharePrice, fPurchaseSharePrice, iNumberOfShares; 

-(float)fCostInDollars; //fPurchaseSharePrice * iNumberOfShares 
{  
    return (fPurchaseSharePrice * iNumberOfShares); 
} 

-(float)fValueInDollars; //fCurrentSharePrice * iNumberOfShares 
{ 
    return (fCurrentSharePrice * iNumberOfShares); 
} 

@end 

'主'

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

@autoreleasepool { 
    StockHolding *Apple = [[StockHolding alloc] init]; 
    [Apple setFPurchaseSharePrice:225]; 
    [Apple setFCurrentSharePrice:300]; 
    [Apple setINumberOfShares:50]; 

    StockHolding *Cisco = [[StockHolding alloc] init]; 
    [Cisco setFPurchaseSharePrice:100]; 
    [Cisco setFCurrentSharePrice:50]; 
    [Cisco setINumberOfShares:75]; 

    StockHolding *WalMart = [[StockHolding alloc] init]; 
    [WalMart setFPurchaseSharePrice:75]; 
    [WalMart setFCurrentSharePrice:150]; 
    [WalMart setINumberOfShares:75]; 

    NSMutableArray *Portfolio = [NSArray arrayWithObjects: Apple, Cisco, WalMart, nil]; 

    for (NSObject *currentStock in Portfolio){ 
     NSLog(@"Purchase Price: %@", currentStock); 
     NSLog(@"Details: %f", [currentStock FPurchaseSharePrice]); // <---Error is on this line. It says "No visible @interface for NSObject declares the selector fPurchaseSharePrice" 
    } 

} 
return 0; 
} 
+0

國會大廈是重要的。而不是'for(NSObject *'try' for(StockHolding *')如果你的對象是正確的類類型,你將得到自動完成。這實際上是對象的子類化的目的是去公共超級,而不是去直接給NSObject這是每個類超類 –

+0

檢查響應標記給出,因爲它在一個答案中呈現相同的場景 –

回答

0

在您的NSLog:FPurchaseSharePrice應該是fPurchaseSharePrice。另外,如果覆蓋 - (NSString *)說明,則打印該對象將產生該字符串,而不是您當前所看到的該對象的指針。

- (NSString *)description 
{ 
    return [NSString stringWithFormat:@"StockHolding: %.2f, %.2f", self.fCostInDollars, self.fValueInDollars]; 
} 

產量:

Purchase Price: StockHolding: 11250.00, 15000.00 

不是:

Purchase Price: <StockHolding: 0x7fe438c0a480> 

最後,而不是鑄造NSObject你的for循環,你可以這樣做:

for (StockHolding *currentStock in Portfolio) 

而且可能不會迴應錯誤將消失。

2

而是做你的for循環

for (StockHolding *currentStock in Portfolio){ 
    NSLog(@"Purchase Price: %@", currentStock); 
    NSLog(@"Details: %f", [currentStock fPurchaseSharePrice]); // <---Error is on this line. It says "No visible @interface for NSObject declares the selector fPurchaseSharePrice" 
} 
+1

我支持這個響應,因爲它在for循環中提供了正確的對象超類,因此具有所有正確的方法和物業訪問者 –

+0

感謝馬克和黑暗天使,工作:) – user1628664

+0

@ user1628664,請將此標記爲接受的答案。 – saji89

0

嗯,就像它說:你沒有使用該名稱的方法。方法名稱在Objective-C中區分大小寫,並且您將屬性(和相應的存取器)聲明爲fPurchaseSharePrice。你想用這個:

NSLog(@"Details: %f", [currentStock fPurchaseSharePrice]);