Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为类实例分配多个变量_Objective C_Variables - Fatal编程技术网

Objective c 为类实例分配多个变量

Objective c 为类实例分配多个变量,objective-c,variables,Objective C,Variables,我定义了一个名为StockHolding的新类,它包含类NSObject的所有方法和实例变量。并添加了3个实例变量: @interface StockHolding : NSObject { float purchaseSharePrice; float currentSharePrice; int numberOfShares; } @interface StockHolding : NSObject @property float purchaseSharePri

我定义了一个名为StockHolding的新类,它包含类NSObject的所有方法和实例变量。并添加了3个实例变量:

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

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

@end
此外,我还添加了实例方法

- (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];
但是接收错误

“StockHolding”没有可见的@interface声明选择器“setPurchaseSharePrice:setCurrentSharePrice:setNumberOfShares:”

我做错了什么?或者只是没有正确使用继承的实例

我看到了一个例子:

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

或者这不起作用?

您可以只创建一条这样的消息,而不是创建三条不同的消息-

- (void)setPurchaseSharePrice:(float)p setCurrentSharePrice:(float)c setNumberOfShares:(int)n;
那么你可以很容易地称之为。此处显示的
NSCalendar
示例与单个消息相同-

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

与创建三条不同的消息不同,您只能创建一条这样的消息-

- (void)setPurchaseSharePrice:(float)p setCurrentSharePrice:(float)c setNumberOfShares:(int)n;
那么你可以很容易地称之为。此处显示的
NSCalendar
示例与单个消息相同-

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

您不能像这样在一组括号中连接多个方法调用。如果要初始化
StockHolding
对象(创建新实例),则需要声明如下方法:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
并以类似的方式实施:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n {
    self = [super init];
    if (self) {
        purchaseSharePrice = p;
        currentSharePrice = c;
        numberOfShares = n;
    }
    return self;
}
这称为初始化器方法。它创建类的新实例并返回它

然后,您可以这样使用它:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
请注意,实际的方法名称等可以是任意的,并且不必以任何方式与实例变量的名称相关联(除非您重写
@property
s的getter和setter,但这是另一回事),因为您仍然必须自己实现该方法,并且可以在其中执行任何您想要的操作


如果您已经创建了这样的实例:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
要设置变量的值,应使用
@property
s而不是实例变量:

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

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

@end
之后,您可以像这样直接设置它们:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;

请注意,要使其工作,您不必声明或实现这些变量的setter和getter。换句话说,您可以安全地删除以下内容:

- (void)setPurchaseSharePrice:(float)p;
- (void)setCurrentSharePrice:(float)c;
- (void)setNumberOfShares:(int)n;
因为如果您使用
@property
s,编译器将自动为您添加它


有关实例变量和
@property

之间差异的更多信息,请阅读及其答案。您不能仅在一组括号中串联多个方法调用。如果要初始化
StockHolding
对象(创建新实例),则需要声明如下方法:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
并以类似的方式实施:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n {
    self = [super init];
    if (self) {
        purchaseSharePrice = p;
        currentSharePrice = c;
        numberOfShares = n;
    }
    return self;
}
这称为初始化器方法。它创建类的新实例并返回它

然后,您可以这样使用它:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
请注意,实际的方法名称等可以是任意的,并且不必以任何方式与实例变量的名称相关联(除非您重写
@property
s的getter和setter,但这是另一回事),因为您仍然必须自己实现该方法,并且可以在其中执行任何您想要的操作


如果您已经创建了这样的实例:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;
要设置变量的值,应使用
@property
s而不是实例变量:

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

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

@end
之后,您可以像这样直接设置它们:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;
StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];
StockHolding *stock = [[StockHolding alloc] init];
StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;

请注意,要使其工作,您不必声明或实现这些变量的setter和getter。换句话说,您可以安全地删除以下内容:

- (void)setPurchaseSharePrice:(float)p;
- (void)setCurrentSharePrice:(float)c;
- (void)setNumberOfShares:(int)n;
因为如果您使用
@property
s,编译器将自动为您添加它

有关StockHolding.h中实例变量和
@property

之间差异的更多信息,请阅读及其答案

@interface StockHolding : NSObject

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

@end
@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
持有股票

#import "StockHolding.h"

@implementation StockHolding ()

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

@end
@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
现在,每一个你想设置股票价值的类

  • 导入库存
    #导入“StockHolding.h”

  • 创建一个对象
    StockHolding*aStockHolding=[[StockHolding alloc]init]

  • 设置值aStockHolding.numberOfShares=1
    [aStockHolding setNumberOfShares:1]

  • 通过合成变量,默认情况下创建了getter和setter方法


    现在,如果要在一个方法调用中设置所有值

  • 在StockHolding.h中声明一个方法

    @interface StockHolding : NSObject
    
    @property (nonatomic, assign) float purchaseSharePrice;
    @property (nonatomic, assign) float currentSharePrice;
    @property (nonatomic, assign) int numberOfShares;
    
    @end
    
    @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
    
  • 定义StockHolding.m中的方法

    #import "StockHolding.h"
    
    @implementation StockHolding ()
    
    @synthesize purchaseSharePrice;
    @synthesize currentSharePrice;
    @synthesize numberOfShares;
    
    @end
    
    @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
    
  • 在上面提到的其他类中创建对象并调用

    StockHolding *aStockHolding = [[StockHolding alloc] init];
    [aStockHolding setPurchaseSharePrice:22.3 andCurrentSharePrice:12.22 withTotalNumberOfShares:120]
    
  • 在库存中

    @interface StockHolding : NSObject
    
    @property (nonatomic, assign) float purchaseSharePrice;
    @property (nonatomic, assign) float currentSharePrice;
    @property (nonatomic, assign) int numberOfShares;
    
    @end
    
    @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
    
    持有股票

    #import "StockHolding.h"
    
    @implementation StockHolding ()
    
    @synthesize purchaseSharePrice;
    @synthesize currentSharePrice;
    @synthesize numberOfShares;
    
    @end
    
    @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
    
    现在,每一个你想设置股票价值的类

  • 导入库存
    #导入“StockHolding.h”

  • 创建一个对象
    StockHolding*aStockHolding=[[StockHolding alloc]init]

  • 设置值aStockHolding.numberOfShares=1
    [aStockHolding setNumberOfShares:1]

  • 通过合成变量,默认情况下创建了getter和setter方法


    现在,如果要在一个方法调用中设置所有值

  • 在StockHolding.h中声明一个方法

    @interface StockHolding : NSObject
    
    @property (nonatomic, assign) float purchaseSharePrice;
    @property (nonatomic, assign) float currentSharePrice;
    @property (nonatomic, assign) int numberOfShares;
    
    @end
    
    @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
    
  • 定义StockHolding.m中的方法

    #import "StockHolding.h"
    
    @implementation StockHolding ()
    
    @synthesize purchaseSharePrice;
    @synthesize currentSharePrice;
    @synthesize numberOfShares;
    
    @end
    
    @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
    
  • 在上面提到的其他类中创建对象并调用

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

  • 也许你想使用KVO或类似的东西?@loldop也许你对objective-C和OOP都是新手的人来说速度有点太快了;)@Cyrille抱歉,如果我太快了,但我没有看到,您是OOP和obj-c的新手:)好的,看看这个,也许它可以帮助您;)对于没有完全掌握obj-C中消息发送的基本概念的人来说,KVO可能太先进了,不是吗