作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在@interface
中,我有这样的声明:
@interface myClass {
NSDictionary * myData;
}
@property (nonatomic, assign) NSDictionary * data;
+ (id) initWithData:(NSDictionary *)data;
@end
在@implementation
中,我有这段代码:
@implementation
@synthesize data;
+ (id) initWithData:(NSDictionary *)freshData {
self = [super init];
if (self) {
self->data = freshData;
}
return self;
}
@end
但我在 self->data
上遇到错误 Incomplete definition of type 'struct objc_class
。
如果我把它改成self.data
,我得到错误Member reference type 'struct objc_class *' is a pointer;您是要使用“->”吗?
如果我删除 self
,我会收到错误“在类方法中访问实例变量‘数据’。”
但是如果我将方法类型从+
(类方法)更改为-
(实例方法),我将无法访问init。
而且我也无法通过将分配从 data
更改为 myData
来修复此错误。
我应该如何构造一个构造函数?
我从中学到(但没有帮助)的链接是:
最佳答案
你也可以这样做
+ (id) initWithData:(NSDictionary *)freshData {
return [[self alloc]initWithData:freshData];
}
- (instancetype) initWithData:(NSDictionary *)freshData {
NSParameterAssert(freshData != nil);
self = [super init];
if (self) {
self.data = freshData;
}
return self;
}
关于ios - 如何在 init 中初始化 Objective C 类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796557/
我是一名优秀的程序员,十分优秀!