2013-07-17 59 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之间差异的更多信息。