에는 다음 @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
하지만 오류가 발생했습니다 Incomplete definition of type 'struct objc_class
에 self->data
.
으로 변경하면 self.data
오류가 발생합니다.Member reference type 'struct objc_class *' is a pointer; did you mean to use '->'?
를 제거하면 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;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다