如何继承对象和初始化:Objective-C
How to inherit object and initialize : Objective-C
我创建了一个名为 Model 的对象,它有一些方法。我想将 Model 导入 Mediator 以在那里使用它的方法。但是当我尝试在 Mediator.m
中执行此操作时,我不断收到错误消息:
Initializer element is not a compile-time constant
& No known class method for selector 'add'
我对这些错误的含义感到困惑。研究它们并没有让事情变得更清楚。
Model.h
@interface Model : NSObject
@property (nonatomic) NSInteger tally;
-(int) add;
@end
Model.m
#import "Model.h"
@interface Model ()
@end
@implementation Model
- (instancetype)init
{
self = [super init];
if (self) {
self.tally = 0;
}
return self;
}
- (int)add {
self.tally = self.tally + 1;
return self.tally;
}
@end
Mediator.h
@interface MediatorController : NSObject
- (int)addOne;
@end
Mediator.m
@interface MediatorController ()
@end
@implementation MediatorController
Model *model = [[Model alloc] init]; <<<<<<< "Initializer element is not a compile-time constant"
- (int)addOne {
return [Model add]; <<<<<< "No known class method for selector 'add"
}
@end
首先,您需要 model
属性 MediatorController
@interface MediatorController : NSObject
@property (strong, nonatomic) Model* model; //<-- Here
- (int)addOne;
- (int)subtractOne;
- (void)setValue:(int)value;
- (int)value;
@end
然后,您需要一个 init
方法——现在,您正试图在实现的顶层编写 model =
,这是行不通的——它需要在一个方法中。
最后,不要在 Model
(即 class)上调用 add
,而是在 self.model
上调用它,这是 [=29 的实例] =].
@implementation MediatorController
- (instancetype)init { //< -- Here
if (self = [super init]) {
self.model = [Model new];
}
return self;
}
- (int)addOne {
return [self.model add];
}
@end
你最终会收到一些编译器警告,因为你还没有实现其余的方法(subtractOne
、setValue
等),但这会让你开始。
我创建了一个名为 Model 的对象,它有一些方法。我想将 Model 导入 Mediator 以在那里使用它的方法。但是当我尝试在 Mediator.m
中执行此操作时,我不断收到错误消息:
Initializer element is not a compile-time constant
& No known class method for selector 'add'
我对这些错误的含义感到困惑。研究它们并没有让事情变得更清楚。
Model.h
@interface Model : NSObject
@property (nonatomic) NSInteger tally;
-(int) add;
@end
Model.m
#import "Model.h"
@interface Model ()
@end
@implementation Model
- (instancetype)init
{
self = [super init];
if (self) {
self.tally = 0;
}
return self;
}
- (int)add {
self.tally = self.tally + 1;
return self.tally;
}
@end
Mediator.h
@interface MediatorController : NSObject
- (int)addOne;
@end
Mediator.m
@interface MediatorController ()
@end
@implementation MediatorController
Model *model = [[Model alloc] init]; <<<<<<< "Initializer element is not a compile-time constant"
- (int)addOne {
return [Model add]; <<<<<< "No known class method for selector 'add"
}
@end
首先,您需要 model
属性 MediatorController
@interface MediatorController : NSObject
@property (strong, nonatomic) Model* model; //<-- Here
- (int)addOne;
- (int)subtractOne;
- (void)setValue:(int)value;
- (int)value;
@end
然后,您需要一个 init
方法——现在,您正试图在实现的顶层编写 model =
,这是行不通的——它需要在一个方法中。
最后,不要在 Model
(即 class)上调用 add
,而是在 self.model
上调用它,这是 [=29 的实例] =].
@implementation MediatorController
- (instancetype)init { //< -- Here
if (self = [super init]) {
self.model = [Model new];
}
return self;
}
- (int)addOne {
return [self.model add];
}
@end
你最终会收到一些编译器警告,因为你还没有实现其余的方法(subtractOne
、setValue
等),但这会让你开始。