如何从内部类访问变量 [英] How to access a variable from inner class

查看:100
本文介绍了如何从内部类访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 //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.

这篇关于如何从内部类访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆