Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从其他类检索变量不起作用_Objective C_Cocos2d Iphone - Fatal编程技术网

Objective c 从其他类检索变量不起作用

Objective c 从其他类检索变量不起作用,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,如果我这样做,是否可以从另一个类获取变量: ----------------------------HelloWorldLayer.h------------------------------- @interface HelloWorldLayer : CCLayer { ... BOOL win; ... } @property (nonatomic,readwrite) BOOL win; @synthesize win; -(void)ccTouchesBegan:(NSSe

如果我这样做,是否可以从另一个类获取变量:

----------------------------HelloWorldLayer.h-------------------------------

@interface HelloWorldLayer : CCLayer
{  

...
BOOL win;
...
}

@property (nonatomic,readwrite) BOOL win;
@synthesize win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (...){

  //do something with with variable win ----> IN <---- if statement
  win = YES;
  }
//win is changed only in if statement
}
-(void)nextLevel:(id)sender{
    NSLog(@"next level");

    HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
    if (obj.win==YES){
        NSLog(@"win = YES");
    }else {
        NSLog(@"win = NO");
    }
    win = NO;
    [[CCDirector sharedDirector] popScene];

}
@interface HelloWorldLayer : CCLayer

@property (nonatomic) BOOL win;
@synthesize win=_win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if (...){
      self.win = YES;
  }

}
----------------------------HelloWorldLayer.m-------------------------------

@interface HelloWorldLayer : CCLayer
{  

...
BOOL win;
...
}

@property (nonatomic,readwrite) BOOL win;
@synthesize win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (...){

  //do something with with variable win ----> IN <---- if statement
  win = YES;
  }
//win is changed only in if statement
}
-(void)nextLevel:(id)sender{
    NSLog(@"next level");

    HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
    if (obj.win==YES){
        NSLog(@"win = YES");
    }else {
        NSLog(@"win = NO");
    }
    win = NO;
    [[CCDirector sharedDirector] popScene];

}
@interface HelloWorldLayer : CCLayer

@property (nonatomic) BOOL win;
@synthesize win=_win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if (...){
      self.win = YES;
  }

}
我是否可以首先在此处获取并设置变量
win
,以便另一个类现在已将win变量分配给
NO
,或者if语句中的win赋值是否未全局处理


如果我在
init
方法中指定变量
NO
,并在函数中更改它,它将只接受在
init
方法中指定的值。。。为什么会这样?

你想考虑使用一个协议。这样,无论HelloWorldLayer是否获胜,都会通知LevelDone。特别是如果HelloWorldLayer是一个长进程并且异步运行的话。下面是一个粗略的例子:

HelloWorldLayer.h

@protocol HelloWorldLayerDelegate
- (void)helloWorldLayer:(HelloWorldLayer *)layer didWin:(BOOL)win;
@end

@interface HelloWorldLayer : CCLayer
{  

...
BOOL win;
...
}

@property (nonatomic,readwrite) BOOL win;
@property (nonatomic, assign) id <HelloWorldLayerDelegate>delegate;

@end
@interface LevelDone <HelloWorldLayerDelegate>
    HelloWorldLayer *_hello;
@end

这是一个混淆使用@synthesis生成的访问器和直接引用实例变量的例子。尝试如下方式声明您的属性:

----------------------------HelloWorldLayer.h-------------------------------

@interface HelloWorldLayer : CCLayer
{  

...
BOOL win;
...
}

@property (nonatomic,readwrite) BOOL win;
@synthesize win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (...){

  //do something with with variable win ----> IN <---- if statement
  win = YES;
  }
//win is changed only in if statement
}
-(void)nextLevel:(id)sender{
    NSLog(@"next level");

    HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
    if (obj.win==YES){
        NSLog(@"win = YES");
    }else {
        NSLog(@"win = NO");
    }
    win = NO;
    [[CCDirector sharedDirector] popScene];

}
@interface HelloWorldLayer : CCLayer

@property (nonatomic) BOOL win;
@synthesize win=_win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if (...){
      self.win = YES;
  }

}
----------------------------HelloWorldLayer.m-------------------------------

@interface HelloWorldLayer : CCLayer
{  

...
BOOL win;
...
}

@property (nonatomic,readwrite) BOOL win;
@synthesize win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (...){

  //do something with with variable win ----> IN <---- if statement
  win = YES;
  }
//win is changed only in if statement
}
-(void)nextLevel:(id)sender{
    NSLog(@"next level");

    HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
    if (obj.win==YES){
        NSLog(@"win = YES");
    }else {
        NSLog(@"win = NO");
    }
    win = NO;
    [[CCDirector sharedDirector] popScene];

}
@interface HelloWorldLayer : CCLayer

@property (nonatomic) BOOL win;
@synthesize win=_win;

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if (...){
      self.win = YES;
  }

}
我认为BOOL值总是默认为False,因此您实际上不需要在init()中设置它,除非您希望它默认为True

无论如何,现在实例变量在类HelloWorldLayer中设置为_win。无论何时您想要设置它,请始终将其称为self.win。在处理属性时,这是一个安全的习惯,以确保不会遇到内存泄漏,因为在处理指针时,生成的访问器将为您释放和保留。如果直接设置实例变量,则必须记住首先释放它

希望这有帮助

如果我在init方法中指定变量NO并在 函数,它将只接受在 初始化方法。。。这到底是为什么

因为您没有使用相同的对象,所以您正在创建一个新对象:

HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
    if (obj.win==YES){
        NSLog(@"win = YES");
    }else {
        NSLog(@"win = NO");
    }
每次运行这行代码时:

HelloWorldLayer *obj = [[HelloWorldLayer alloc]init];
…创建HelloWorldLayer的新实例。它将运行init方法中的代码,因为您正在向它发送
init
消息。这就是为什么上面的代码将记录在init方法中设置的任何值


您想要的是访问HelloWorldLayer的现有实例,而不是创建该类的新实例。我相信@Jeremy已经为您提供了一个令人满意的解决方案。另一种方法是将HelloWorldLayer类转换为单例。

好的,非常感谢。。。一个问题:如果我把
@protocol HelloWorldLayerDelegate-(void)helloWorldLayer:(helloWorldLayer*)层放到didWin:(BOOL)win
@界面之前
它将无法识别类型
(HelloWorldLayer*)
谢谢。我以前就想到过,但我(错误地)被告知我实际上可以访问它们。