如何从内部类访问变量
//MainClass.m
@interface InnerClass : NSObject{
}
@end
@implementation InnerClass
-(void)run{
while(isActive){//want to access this variable which defined in MainClass
//do something
}
}
@end
@interface MainClass : NSObject{
BOOL isActive;
}
@end
@implementation MainClass
@end
我有MainClass,它有一个内部类(InnerClass).我想从内部类访问MainClass类中定义的布尔类型变量(isActive).我想做的是,内部类将在单独的线程上运行,并将继续检查主类上的isActive变量,如果isActive为false,则它将停止运行新线程. /p>
I have MainClass and it has an inner class (InnerClass). I want to access the boolean type variable (isActive) defined in MainClass class from the inner class. What I am trying to do is that the inner class will run on a separate thread and will keep checking the isActive variable on the main class and if isActive is false then it will stop running the new thread.. Thanks in advance...
Objective-C没有内部类.考虑将isActive设置为MainClass的属性,为InnerClass提供指向MainClass实例的指针,然后让InnerClass轻松访问该属性.
Objective-C doesn't have inner classes. Consider making isActive a property of MainClass, give InnerClass a pointer to an instance of MainClass, and let InnerClass simply access the property.