2013-07-17 57 views
1

我已經定義了一個名爲StockHolding的新類,該類具有類NSObject的所有方法和實例變量。並加入3個實例變量:將多個變量分配給類實例

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

而且,我已經添加了實例方法

- (void)setPurchaseSharePrice:(float)p; 
- (void)setCurrentSharePrice:(float)c; 
- (void)setNumberOfShares:(int)n; 

的main.m文件,我創建類的實例

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

,並希望用變量創建和反對

StockHolding *a = [stock setPurchaseSharePrice:2.30 setCurrentSharePrice:4.50 setNumberOfShares:40]; 

但收到錯誤

爲 '持股' 不可見@interface聲明選擇 'setPurchaseSharePrice:setCurrentSharePrice:setNumberOfShares:'

什麼,我做錯了什麼?或者它不適當地使用繼承的實例。

我看到例如:

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSUInteger day = [cal ordinalityOfUnit:NSDayCalendarUnit 
           inUnit:NSMonthCalendarUnit 
           forDate:now]; 

或者這是不行的?

+0

也許你想使用KVO或類似的東西? – gaussblurinc

+0

@loldop或許你對某個明顯對Objective-C陌生的人有點太快,並且可能對OOP一般;) – Cyrille

+0

@Cyrille對不起,如果我速度太快,但我沒看到,你是OOP和obj-c的新手:)好的,看看這個,也許它可以幫助你;)[魅力關於objective-c對象處理](http://developer.apple.com/library/mac/#documentation /cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html) – gaussblurinc

回答

0

在StockHolding.h

@interface StockHolding : NSObject 

@property (nonatomic, assign) float purchaseSharePrice; 
@property (nonatomic, assign) float currentSharePrice; 
@property (nonatomic, assign) int numberOfShares; 

@end 

在StockHolding.m

#import "StockHolding.h" 

@implementation StockHolding() 

@synthesize purchaseSharePrice; 
@synthesize currentSharePrice; 
@synthesize numberOfShares; 

@end 

現在,要設置持股的值所有其他類,

  1. 導入持股#import "StockHolding.h"

  2. 創建一個對象StockHolding *aStockHolding = [[StockHolding alloc] init];

  3. 設置值aStockHolding.numberOfShares = 1;[aStockHolding setNumberOfShares:1]

通過合成變量,你必須默認創建的getter和setter方法。現在


,如果你要設置的所有值在一個方法調用..

  1. 聲明一個方法StockHolding.h

    @interface StockHolding : NSObject 
    
    @property (nonatomic, assign) float purchaseSharePrice; 
    @property (nonatomic, assign) float currentSharePrice; 
    @property (nonatomic, assign) int numberOfShares; 
    
    - (void)setPurchaseSharePrice:(float)iPurchase andCurrentSharePrice:(float)iCurrent withTotalNumberOfShares:(int)iTotal; 
    
    @end 
    
  2. 在股份制定義方法。米

    @implementation StockHolding() 
    
    @synthesize purchaseSharePrice; 
    @synthesize currentSharePrice; 
    @synthesize numberOfShares; 
    
    - (void)setPurchaseSharePrice:(float)iPurchase andCurrentSharePrice:(float)iCurrent withTotalNumberOfShares:(int)iTotal { 
        self.purchaseSharePrice = iPurchase; 
        self.currentSharePrice = iCurrent; 
        self.numberOfShares = iTotal; 
    } 
    @end 
    
  3. 創建其他類的對象,如上所述,並呼籲

    StockHolding *aStockHolding = [[StockHolding alloc] init]; 
    [aStockHolding setPurchaseSharePrice:22.3 andCurrentSharePrice:12.22 withTotalNumberOfShares:120] 
    
+0

非常感謝你,確切地說我在找什麼。 – oleglestat

+0

自從Xcode 4.4以來,'@ synthesize'語句可以省略。 –

0

而是創建三個不同的消息,你可以像這樣只創建一個消息 -

- (void)setPurchaseSharePrice:(float)p setCurrentSharePrice:(float)c setNumberOfShares:(int)n; 

然後你就可以輕鬆地調用它。你這裏顯示的NSCalendar的例子是同樣的事情,一個消息 -

- (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date 
1

你不能只是連接多個方法在一組這樣的括號調用。如果你想初始化(創建的新實例)一個StockHolding對象,你需要聲明一個像這樣的方法:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n; 

而且同樣實現它這樣:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n { 
    self = [super init]; 
    if (self) { 
     purchaseSharePrice = p; 
     currentSharePrice = c; 
     numberOfShares = n; 
    } 
    return self; 
} 

這是稱爲初始化者方法。它創建該類的新實例並將其返回。

然後,您可以使用它像這樣:

StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40]; 

注意實際的方法名稱等可以是任意的,不以任何方式與您的實例變量的名字相關聯(除非你爲@property重寫getter和setter,但這是另一回事),因爲你仍然必須自己實現這個方法,並且你可以在那裏做任何你想做的事情。


如果您已經創建了這樣一個實例:

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

,並要設置的變量的值,你應該使用@property s,而不是實例變量:

@interface StockHolding : NSObject 

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

@end 

之後,你可以像這樣直接設置它們:

StockHolding *stock = [[StockHolding alloc] init]; 
stock.purchaseSharePrice = whatever; 

[stock setPurchaseSharePrice:2.30]; 
[stock setCurrentSharePrice:4.50]; 

注意,對於這個工作,必須申報或實施的getter和setter方法對這些變量。換句話說,你可以安全地刪除這樣的:

- (void)setPurchaseSharePrice:(float)p; 
- (void)setCurrentSharePrice:(float)c; 
- (void)setNumberOfShares:(int)n; 

因爲如果使用@property S中的編譯器會自動添加它。

閱讀this question及其答案,以獲取有關實例變量和@property之間差異的更多信息。