안녕하세요,이 포럼에서이 질문이 여러 번 질문되었다는 것을 알고 있습니다. 해당 게시물을 살펴본 후 게시물 중 하나에서 문제에 대한 부분적인 해결책을 찾았지만이 게시물 Objective-C 에서 한 가지 의심이 있습니다. : 다른 클래스의 속성에 액세스 .
주어진 대답으로 속성 값에 액세스 할 수 있었지만 자식 클래스의 다른 인스턴스 메서드에서 해당 속성의 동일한 값을 사용할 수 없었습니다. 누구나 동일한 작업을 수행하는 방법에 대한 몇 가지 예를 제공 할 수 있습니다.
추신 : 나는 이것이 다른 게시물에 대해 묻는 매우 기본적인 것임을 이해합니다. 나는이 질문에 대해 언급 할만 큼 충분한 평판을 얻지 못하기 때문에 과거에서 이것을 쳤기 때문에 이것을 도와주세요. 3 일, 어떤 도움이라도 대단히 감사하겠습니다.
감사합니다
코드 업데이트
@interface ClassA : SomeSuperClass
@property (some Attributes) ClassB *classB;
@property (some Attributes) NSString *someString;
@end
@implementation
-(id)init {
if (self = [super init]) {
_classB = [[ClassB alloc]initWithParent:self];
}
}
@end
@class ClassA;
@interface ClassB : SomeSuperClass
@property (nonatomic, weak) ClassA *classA;
-(id)initWithParent:(ClassA*)parent;
@end
#import "ClassA.h"
@implementation
-(void)viewDidLoad{
NSLog(@"%@",self.classA.someString); //here I get null
}
-(id)initWithParent:(ClassA*)parent {
if (self = [super init]) {
_classA = parent;
NSLog(@"%@", self.classA.someString); //perfectly legal and prints the string value
}
}
단일 인스턴스를 생성하는 단일 인스턴스를 사용할 수 있고 다른 클래스는 속성에 액세스하기 위해 동일한 인스턴스를 사용할 수 있다고 생각합니다.
전의:
+(id)singletonInstance
{
static classA *classA = nil;
static dispatch_oce_t onceToken;
dispatch_once(&onceToken, ^{
// if the instance is not there then create an instance and init one.
classA = [[self alloc] init];
});
return classA;
}
// in the same class .m file viewDidLoad add the below code
//classA.m
classA *classA = [classA sharedInstance]; // this will be the instance which will be called by other classes (i.e classB ..etc).
코드를 테스트했으며 모든 쿼리에 대해 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다