Objective c 为类实例分配多个变量
我定义了一个名为StockHolding的新类,它包含类NSObject的所有方法和实例变量。并添加了3个实例变量: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
@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 setNumberOfShares:1]
现在,如果要在一个方法调用中设置所有值
@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 *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 setNumberOfShares:1]
现在,如果要在一个方法调用中设置所有值
@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 *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可能太先进了,不是吗